Forum guidelines
  • Home
  • »
  • LINQ
  • »
  • How to dynamically cast a Table to a List
AuthorQuestion
How to dynamically cast a Table to a List
Posted By Slim Sjakie on 12 Dec 2012

I have the following piece of code:

ITable tResults = myDataContext.GetTable(myObjectType);
                    
Type myListType = typeof(List<>).MakeGenericType(myObjectType);

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<type> where Type comes from the myObjectType Type variable.

my Question: How do I convert/cast my Linq.Table to a strongly typed List List<myType> ?


AuthorReply
Re: How to dynamically cast a Table to a List
Posted By Shambhu on 16 Dec 2012  
Hi,

//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<DateTime>("RegDate").ToString("yyyy-MM")) == Convert.ToDateTime("2012-09-01")
                                 group n by new
                                 {
                                     WHCode = n.Field<string>("WHCode"),
                                     WHLocation = n.Field<string>("WHLocation"),
                                     WHName = n.Field<string>("WHName"),
                                     RegDate = Convert.ToDateTime(n.Field<DateTime>("RegDate").ToString("yyyy-MM"))
                                 } 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

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.