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 IEnumerable List in Asp.net using C#, VB.NET

Feb 3, 2015
Introduction

Here I will explain how to convert datatable to IEnumerable list in
c#, vb.net using as.npet. IEnumerable list is a base for all collections and its having ability to loop through the collection by using current property, MoveNext and Reset methods in c#, vb.net.

Description:
  
In previous articles I explained
convert datatable to json string in c#, convert datatable to generic list using LINQ, convert datatable to xml string in asp.net, convert datatable or dataset to array in asp.net and many articles relating to asp.net, c#, vb.net, jQuery and JavaScript and. Now I will explain how to convert datatable to IEnumerable list in c#, vb.net using as.npet.

To convert datatable to IEnumerable we need to write the code like as shown below

C# Code


var data = dt.AsEnumerable().Select(row =>
new
{
UserId = row["UserId"].ToString(),
UserName = row["UserName"].ToString(),
Education = row["Education"].ToString()
});
VB.NET Code


Dim data = dt.AsEnumerable().[Select](Function(row) New With { _
Key .UserId = row("UserId").ToString(), _
Key .UserName = row("UserName").ToString(), _
Key .Education = row("Education").ToString() _
})
If you want to see it in complete example open your Default.aspx page and write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to Get IEnumerable values in asp.net using c#</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnGetValues" Text="Get Values" runat="server"
onclick="btnGetValues_Click" /><br />

<asp:Label ID="lblval" runat="server"></asp:Label>
</div>
</form>
</body>
</html>

Now open your code behind file and write the code like as shown below

C# Code


using System;
using System.Linq;
using System.Data;

public partial class GetIenumerablevalues : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnGetValues_Click(object sender, EventArgs e)
{
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");
var data = dt.AsEnumerable().Select(row =>
new
{
UserId = row["UserId"].ToString(),
UserName = row["UserName"].ToString(),
Education = row["Education"].ToString()
});
foreach (var row in data)
{
lblval.Text += "UserId: "+row.UserId+", Name: " +row.UserName+", Value: "+row.Education+"<br/>";
}
}
}
VB.NET Code


Imports System.Linq
Imports System.Data
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub btnGetValues_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As New DataTable()
dt.Columns.Add("UserId", GetType(Int32))
dt.Columns.Add("UserName", GetType(String))
dt.Columns.Add("Education", GetType(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")

Dim data = dt.AsEnumerable().[Select](Function(row) New With { _
Key .UserId = row("UserId").ToString(), _
Key .UserName = row("UserName").ToString(), _
Key .Education = row("Education").ToString() _
})
For Each row In data
lblval.Text += "UserId: " + row.UserId + ", Name: " + row.UserName + ", Value: " + row.Education + "<br/>"
Next
End Sub
End Class
Demo

C# - Convert Datatable to IEnumerable List in Asp.net using C#, VB.NET


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 :

Anonymous said...

nice job suresh..

Prasunjeet Soni said...

well defined example.

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.