I have the following piece of code:
So, I get the table data from object type which gives me a Linq.Table object (line 1).
Now, in line 2 I dynamically create a List
my Question: How do I convert/cast my Linq.Table to a strongly typed List List
I have the following piece of code:
So, I get the table data from object type which gives me a Linq.Table object (line 1).
Now, in line 2 I dynamically create a List
my Question: How do I convert/cast my Linq.Table to a strongly typed List List
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
ShambhuPosted Dec 16, 2012, 8:27 AM
//Declare table
DataTable dtWarehouse = new DataTable("Warehouse");
dtWarehouse.Columns.Add("WHCode", System.Type.GetType("System.String")); //GetType(require object), get resolved at runtime
dtWarehouse.Columns.Add("WHLocation", typeof(string)); //typeof(require class), get resolved at compile time.
dtWarehouse.Columns.Add("WHName", typeof(string));
dtWarehouse.Columns.Add("RegDate", typeof(DateTime));
dtWarehouse.Rows.Add("W001", "BANGALORE", "WH01", Convert.ToDateTime("01-09-2012"));
dtWarehouse.Rows.Add("W002", "KOLKATA", "WH02", Convert.ToDateTime("02-09-2012"));
dtWarehouse.Rows.Add("W003", "DELHI", "WH03", new DateTime(2012, 07, 01));
//Use data table to convert storngly typed list of class warehouse
var grpWarehouseDt = from n in dtWarehouse.AsEnumerable()
where Convert.ToDateTime(n.Field
group n by new
{
WHCode = n.Field
WHLocation = n.Field
WHName = n.Field
RegDate = Convert.ToDateTime(n.Field
} into grpWH
select new
{
WHCode = grpWH.Key.WHCode,
WHLocation = grpWH.Key.WHLocation,
WHName = grpWH.Key.WHName,
RegDate = grpWH.Key.RegDate
};
class Warehouse
{
public string WHName { get; set; }
public string WHCode { get; set; }
public string WHLocation { get; set; }
}
Regards,
Shambhu