How can I give an alias name to fields selected from a DataTable
using LINQ?
views:
48answers:
2
+1
A:
var query =dataTable.Rows.Cast<DataRow>().Select(row => new
{
aliasName1 = row["Column1"].ToString(),
aliasName2 = row["Column2"].ToString()
});
anishmarokey
2010-08-18 04:58:29
@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
2010-08-18 05:14:34
+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
2010-08-18 04:59:59