Fill a DataSet or a DataTable from a LINQ query resultset

Asked By 0 points N/A Posted on -
qa-featured

How do you expose a LINE query as an ASMX web service? Usually, from the business tier, I can return a typed DataSet or DataTable which can be serialized for transport over ASMX.

How can I do the same for a LINE query? Is there a way to populate a typed DataSet or DataTable via a LINE query?

Code is attached.

How can I get the resultset of a LINE query into a DataSet or DataTable? Alternatively, is the LINE query serializeable so that I can expose it as an ASMX web service?

SHARE
Answered By 0 points N/A #86303

Fill a DataSet or a DataTable from a LINQ query resultset

qa-featured

I create a function which take query of line and return datatable.

 public DataTable LINEToDataTable<T>(IEnumerable<T> varlist)
{
     DataTable dtReturn = new DataTable();
 
     // column names 
     PropertyInfo[] oProps = null;
 
     if (varlist == null) return dtReturn;
 
     foreach (T rec in varlist)
     {
          // Use reflection to get property names, to create table, Only first time, others 
       
          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;
}
 
 
Regards,
 
desh rathore

Related Questions