none
Linq to Datatable Master detail RRS feed

  • Question

  • Hi,

    I have two table order and orderdetail, I am using entity frame work. I am getting result and then converting Linq to datatable so I can display the data in crystal report. To convert Linq to data table I am using the following code:

     public DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
    {
       DataTable dtReturn = new DataTable();
    
       // column names 
       PropertyInfo[] oProps = null;
    
       if (varlist == null) return dtReturn;
    
       foreach (T rec in varlist)
       {
         
         if (oProps == null)
         {
            oProps = ((Type)rec.GetType()).GetProperties();
            foreach (PropertyInfo pi in oProps)
            {
              Type colType = pi.PropertyType;
    
              if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()  
              ==typeof(Nullable<>)))
               {
                 colType = colType.GetGenericArguments()[0];
               }
    
              dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
            }
         }
    
         DataRow dr = dtReturn.NewRow();
    
         foreach (PropertyInfo pi in oProps)
         {
            dr[pi.Name] = pi.GetValue(rec, null) == null ?DBNull.Value :pi.GetValue
            (rec,null);
         }
    
         dtReturn.Rows.Add(dr);
       }
       return dtReturn;
    }
    


    The datatable return by this method contain all information in Order table but I cant get in to the order detail table, is there any way to get to the order detail table.

    Thanks

     

    Friday, June 24, 2011 9:56 AM

Answers

  • Hi xFahadx;

    In the query are you retuning the orderdetail record as well? For example :

    var orders = from o in order.Include("orderdetail")
                       select o;

    The Include method will Eager load the orderdetail for each of the orders.

     

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, June 24, 2011 4:25 PM

All replies

  • Hi xFahadx;

    In the query are you retuning the orderdetail record as well? For example :

    var orders = from o in order.Include("orderdetail")
                       select o;

    The Include method will Eager load the orderdetail for each of the orders.

     

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, June 24, 2011 4:25 PM
  • Any update? Would you mind letting us know how it goes?

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 27, 2011 6:49 AM