Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice: Convert LINQ Query result to a DataTable without looping

Tags:

What is the best practice to convert LINQ-Query result to a new DataTable?
can I find a solution better than foreach every result item?

EDIT AnonymousType

var rslt = from eisd in empsQuery            join eng in getAllEmployees()            on eisd.EMPLOYID.Trim() equals eng.EMPLOYID.Trim()            select new            {                eisd.CompanyID,                eisd.DIRECTID,                eisd.EMPLOYID,                eisd.INACTIVE,                eisd.LEVEL,                eng.EnglishName            }; 

EDIT 2: I got exception:

Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator.

as I try to execute the query and found the solution here IEnumerable.Except wont work, so what do I do?
and Need linq help

like image 878
Rami Alshareef Avatar asked Dec 16 '10 12:12

Rami Alshareef


People also ask

Can we use LINQ to query against a DataTable?

Can we use linq to query against a DataTable? Explanation: We cannot use query against the DataTable's Rows collection, since DataRowCollection doesn't implement IEnumerable<T>. We need to use the AsEnumerable() extension for DataTable.

How return DataTable from LINQ?

To query datatable using linq we call the AsEnumerable() method of the DataTable. Calling this method on the DataTable returns an object which implements the IEnumerable<T> interface. Now we can perform LINQ queries on this object. Add a reference to the System.

How LINQ queries converted into SQL queries?

LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.

Can LINQ query work with Array?

Yes it supports General Arrays, Generic Lists, XML, Databases and even flat files. The beauty of LINQ is uniformity.


2 Answers

Use Linq to Dataset. From the MSDN : Creating a DataTable From a Query (LINQ to DataSet)

// Query the SalesOrderHeader table for orders placed  // after August 8, 2001. IEnumerable<DataRow> query =     from order in orders.AsEnumerable()     where order.Field<DateTime>("OrderDate") > new DateTime(2001, 8, 1)     select order;  // Create a table from the query. DataTable boundTable = query.CopyToDataTable<DataRow>(); 

If you have anonymous types :

From the Coder Blog : Using Linq anonymous types and CopyDataTable

It explains how to use MSDN's How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow

like image 138
LaGrandMere Avatar answered Oct 08 '22 06:10

LaGrandMere


Converting Query result in DataTables Generic Function

 DataTable ddt = new DataTable();   ddt = LINQResultToDataTable(query);      public DataTable LINQResultToDataTable<T>(IEnumerable<T> Linqlist)     {         DataTable dt = new DataTable();           PropertyInfo[] columns = null;          if (Linqlist == null) return dt;          foreach (T Record in Linqlist)         {              if (columns == null)             {                 columns = ((Type)Record.GetType()).GetProperties();                 foreach (PropertyInfo GetProperty in columns)                 {                     Type colType = GetProperty.PropertyType;                      if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()                     == typeof(Nullable<>)))                     {                         colType = colType.GetGenericArguments()[0];                     }                      dt.Columns.Add(new DataColumn(GetProperty.Name, colType));                 }             }              DataRow dr = dt.NewRow();              foreach (PropertyInfo pinfo in columns)             {                 dr[pinfo.Name] = pinfo.GetValue(Record, null) == null ? DBNull.Value : pinfo.GetValue                 (Record, null);             }              dt.Rows.Add(dr);         }         return dt;     }   
like image 31
Huzaifa Khan Avatar answered Oct 08 '22 08:10

Huzaifa Khan