tags:

views:

48

answers:

2

How can I give an alias name to fields selected from a DataTable using LINQ?

+1  A: 
var query =dataTable.Rows.Cast<DataRow>().Select(row => new 
{
   aliasName1 = row["Column1"].ToString(),
  aliasName2 = row["Column2"].ToString()
 });
anishmarokey
@anishmarokey This won't compile because your missing the cast that I have included in my answer and you bracketting is off as well. `System.Data.DataRowCollection' does not contain a definition for 'Select'`.
Kelsey
+3  A: 
var output = dataTable.Rows.Cast<DataRow>().Select(r => new 
    {
       alias1 = r["Column1"].ToString(),
       alias2 = r["Column2"].ToString(),
       alias3 = r["Column3"].ToString(),
       /// etc...
    });
Kelsey