Header add

In this article we will learn about How to convert LINQ query result to DataTable using C#. For example we have the List of Customers and we want to extract this customers using Linq and convert the result to datatable in C#. Following below steps we can achieve that. Please read our all C# articles here.

Step-1

Let's first create the Model that hold the Customer details like below,

public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Country { get; set; }
}
view raw Customer.cs hosted with ❤ by GitHub

Create another Model class that will responsible to convert Linq result to DataTable.
  • 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 and it returned.
public class Utility
{
//Generic function to convert Linq query to DataTable.
public DataTable ConvertLinqToDataTable<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;
}
}
view raw Utility.cs hosted with ❤ by GitHub


Step-2

On below we added some dummy data into List<customer> and using Linq query we extract and call the convert method to use in dataTable.

public partial class Default : System.Web.UI.Page
{
Utility _util = new Utility();
protected void Page_Load(object sender, EventArgs e)
{
getCustomers();
}
public void getCustomers()
{
List<Customer> customers = new List<Customer>();
customers.Add(new Customer { CustomerId = 1, Name = "John Smith", Age=59, Country = "United States" });
customers.Add(new Customer { CustomerId = 2, Name = "Aakash Burman", Age = 30, Country = "India" });
customers.Add(new Customer { CustomerId = 3, Name = "Tim David", Age = 24, Country = "UK" });
customers.Add(new Customer { CustomerId = 4, Name = "Kadir syed", Age = 36, Country = "Pakistan" });
customers.Add(new Customer { CustomerId = 4, Name = "Ravi", Age = 34, Country = "India" });
//Linq query.
var query = from customer in customers
select customer;
DataTable dt = _util.ConvertLinqToDataTable<Customer>(query);
}
}
view raw Default.cs hosted with ❤ by GitHub


Output

Now debug the application we can see in debug visualizer the result like below. On the next article we can see how to display it in Gridview.

linq-to-datatable


Previous Post Next Post