In this article I will explain with an example, how to convert LINQ query result to DataTable using C# and VB.Net.
Class
A generic List Collection of the following class will be queried using LINQ and its result will be converted into DataTable.
C#
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
VB.Net
Public Class Customer
Public Property CustomerId As Integer
Public Property Name As String
Public Property Country As String
End Class
Namespaces
You will need to import the following namespaces.
C#
using System.Linq;
using System.Data;
using System.Reflection;
VB.Net
Imports System.Linq
Imports System.Data
Imports System.Reflection
Generic function to convert LINQ query result to DataTable
The following function accepts Innumerable collection as input parameter.
First, a DataTable is created by determining the name of the Class.
Then the properties of the Class are read and then Columns are created in the DataTable after determining the names of the properties.
Now, a loop is executed over the items of the IEnumerable collection and inside the loop, a DataRow is created with values and added to the DataTable.
Finally, the DataTable is returned.
C#
//Generic function to convert Linq query to DataTable.
public DataTable LinqToDataTable<T>(IEnumerable<T> items)
{
//Createa DataTable with the Name of the Class i.e. Customer class.
DataTable dt = new DataTable(typeof(T).Name);
//Read all the properties of the Class i.e. Customer class.
PropertyInfo[] propInfos = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
//Loop through each property of the Class i.e. Customer class.
foreach (PropertyInfo propInfo in propInfos)
{
//Add Columns in DataTable based on Property Name and Type.
dt.Columns.Add(new DataColumn(propInfo.Name, propInfo.PropertyType));
}
//Loop through the items if the Collection.
foreach (T item in items)
{
//Add a new Row to DataTable.
DataRow dr = dt.Rows.Add();
//Loop through each property of the Class i.e. Customer class.
foreach (PropertyInfo propInfo in propInfos)
{
//Add value Column to the DataRow.
dr[propInfo.Name] = propInfo.GetValue(item, null);
}
}
return dt;
}
VB.Net
'Generic function to convert Linq query to DataTable.
Public Function LinqToDataTable(Of T)(ByVal items As IEnumerable(Of T)) As DataTable
'Createa DataTable with the Name of the Class i.e. Customer class.
Dim dt As DataTable = New DataTable(GetType(T).Name)
'Read all the properties of the Class i.e. Customer class.
Dim propInfos As PropertyInfo() = GetType(T).GetProperties(BindingFlags.Public Or BindingFlags.Instance)
'Loop through each property of the Class i.e. Customer class.
For Each propInfo As PropertyInfo In propInfos
'Add Columns in DataTable based on Property Name and Type.
dt.Columns.Add(New DataColumn(propInfo.Name, propInfo.PropertyType))
Next
'Loop through the items of the Collection.
For Each item As T In items
'Add a new Row to DataTable.
Dim dr As DataRow = dt.Rows.Add()
'Loop through each property of the Class i.e. Customer class.
For Each propInfo As PropertyInfo In propInfos
'Add value Column to the DataRow.
dr(propInfo.Name) = propInfo.GetValue(item, Nothing)
Next
Next
Return dt
End Function
Converting LINQ query result to DataTable using C# and VB.Net
Inside the Page Load event, a Generic List collection of Customer class objects is created with some dummy values.
Then, a LINQ query is executed over the Generic List collection of Customer class objects.
Finally, the LINQ query is converted to DataTable.
C#
protected void Page_Load(object sender, EventArgs e)
{
//List collection with some data.
List<Customer> customers = new List<Customer>();
customers.Add(new Customer { CustomerId = 1, Name = "John Hammond", Country = "United States" });
customers.Add(new Customer { CustomerId = 2, Name = "Mudassar Khan", Country = "India" });
customers.Add(new Customer { CustomerId = 3, Name = "Suzzane Mathews", Country = "France" });
customers.Add(new Customer { CustomerId = 4, Name = "Robert Schidner", Country = "Russia" });
//Linq query.
var query = from customer in customers
select customer;
DataTable dt = LinqToDataTable<Customer>(query);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
'List collection with some data.
Dim customers As List(Of Customer) = New List(Of Customer)()
customers.Add(New Customer With {.CustomerId = 1, .Name = "John Hammond", .Country = "United States"})
customers.Add(New Customer With {.CustomerId = 2, .Name = "Mudassar Khan", .Country = "India"})
customers.Add(New Customer With {.CustomerId = 3, .Name = "Suzzane Mathews", .Country = "France"})
customers.Add(New Customer With {.CustomerId = 4, .Name = "Robert Schidner", .Country = "Russia"})
'Linq query.
Dim query = From customer In customers
Select customer
Dim dt As DataTable = LinqToDataTable(Of Customer)(query)
End Sub
Screenshot
Downloads