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
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.
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.
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.
Yes it supports General Arrays, Generic Lists, XML, Databases and even flat files. The beauty of LINQ is uniformity.
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
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With