Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - Convert Datatable to List Example using LINQ

Nov 3, 2014
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

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

2 comments :

Unknown said...
This comment has been removed by a blog administrator.
test said...

Such a Wonderfull support. thanks a lot.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.