convert linq query results to datatable C#

前端 未结 3 371
无人及你
无人及你 2020-12-21 05:06

I want to convert the linq query results to datatable so that I can assign datatable to GridView to show it on asp page.

However I am not able to convert the results

3条回答
  •  有刺的猬
    2020-12-21 05:33

    First declare a new DataTable and add columns, in this :

    DataTable dt = new DataTable();
    dt.Columns.Add("FirstName");
    dt.Columns.Add("LastName");
    DataRow row = null;
    

    Now I simply iterate through the query and fill a DataTable:

    foreach (var rowObj in query)
    {
        row = dt.NewRow();
        dt.Rows.Add(rowObj.FirstName, rowObj.LastName);
    }
    

提交回复
热议问题