Monday, November 3, 2014

C# - Convert Datatable to List Example using LINQ

Introduction

Here I will explain how to convert datatable to list in 
c# using linq with example or convert datatable to generic list  in c# using linq with example and other ways to convert datatable to list in c# and vb.net.

Description: 
   
In previous articles I explained convert string to xml in c#, convert datatable to xml string in c#,convert datatable to json string in c#, convert datatable/dataset to arraylist in c# and many articles relating to 
asp.net, c#,vb.net and jQuery. Now I will explain how to convert datatable to list in c# using linq with example and other ways to convert datatable to list in c#.

In c# we can convert datatable to list in different ways

Method1


List<UserDetails> list=new List<UserDetails>();
for (int i = 0; i < dt.Rows.Count; i++)
{
UserDetails userinfo = new UserDetails();
userinfo.UserId = dt.Rows[i]["UserId"].ToString();
userinfo.UserName = dt.Rows[i]["UserName"].ToString();
userinfo.Education = dt.Rows[i]["Education"].ToString();
list.Add(userinfo);
}
Method2

Using LINQ lambda expression


List<UserDetails> list=new List<UserDetails>();
list = (from DataRow row in dt.Rows

select new UserDetails()
{
UserId = row["UserId"].ToString(),
UserName = row["UserName"].ToString(),
Education = row["Education"].ToString()
}).ToList();

If you want to see it in complete example check below code


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Convert Datatable to List in c#</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvdetails" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
Now open code behind file and write the following code


using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

public partial class convertdatatabletolist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDetails();
        }
    }
    private void BindDetails()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("UserId", typeof(Int32));
        dt.Columns.Add("UserName", typeof(string));
        dt.Columns.Add("Education", typeof(string));
        dt.Rows.Add(1, "Suresh Dasari", "B.Tech");
        dt.Rows.Add(2, "Rohini Dasari", "Msc");
        dt.Rows.Add(3, "Madhav Sai", "MS");
        dt.Rows.Add(4, "Praveen", "B.Tech");
        dt.Rows.Add(6, "Sateesh", "MD");
        dt.Rows.Add(7, "Mahesh Dasari", "B.Tech");
        dt.Rows.Add(8, "Mahendra", "CA");
        List<UserDetails> list = new List<UserDetails>();

        list = (from DataRow row in dt.Rows

                select new UserDetails()
                {
                    UserId = row["UserId"].ToString(),
                    UserName = row["UserName"].ToString(),
                    Education = row["Education"].ToString()
                }).ToList();

        gvdetails.DataSource = list;
        gvdetails.DataBind();
    }
    public class UserDetails
    {
        public string UserId { get; set; }
        public string UserName { get; set; }
        public string Education { get; set; }
    }
}
Demo

Bind Generic List to Gridview in csharp

No comments:

Post a Comment