How to convert IQueryable to DataTable
17,490
CopyToDataTable
requires collection of DataRow
objects. See How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow to solve this problem.
UPDATE: If your entity has nullable field, you can modify ObjectShredder.ExtendTable
method. Find place where new columns are added to table, and remove second parameter, which provides type of data in column (DataColumn class does not support nullable data types):
foreach (PropertyInfo p in type.GetProperties())
{
if (!_ordinalMap.ContainsKey(p.Name))
{
DataColumn dc = table.Columns.Contains(p.Name) ? table.Columns[p.Name]
// do not provide column type
: table.Columns.Add(p.Name);
_ordinalMap.Add(p.Name, dc.Ordinal);
}
}
Author by
steve
Updated on July 20, 2022Comments
-
steve almost 2 years
I wrote the query using LinQ and I used the
CopyToDataTable
method. At that line it is showing implicit conversion type error from my database type toSystem.Data.DataRow
.var query = from i in dbContext.Personaldetails where i.ID == 1 select i; return query.CopyToDataTable();
Any suggestions?
-
steve about 12 yearsI have implemented that logic but at the query.copytodatatable it is showing error that can't implicit conversion type.
-
Sergey Berezovskiy about 12 years@nanicharan make sure you use
CustomLINQtoDataSetMethods
, not those from LINQ to dataset. -
steve almost 12 yearsI used the same method.i'll send my code.can u please check it once.
-
Sergey Berezovskiy almost 12 years@nanicharan I created tables recently with exactly same code. Just copy-paste it from msdn. And make sure you use
CustomLINQtoDataSetMethods
extension. E.g. you can call it directlyCustomLINQtoDataSetMethods.CopyToDataTable(query)
-
steve almost 12 yearsvar query = from i in dbContext.Personaldetails where i.ID == 1 select i; DataTable table = query.CopyToDataTable(); I used in this way.I wrote this query inside a method.next to that method i declared CustomLINQtoDataSetMethods
-
steve almost 12 yearsthanks, i changed the code.the error rectified.now i want to load the data into my dhtmlx grid.
-
steve almost 12 years,error rectified at that statement but,in the CustomLINQtoDataSetMethods, i declared two public static DataTable CopyToDataTable<T>(this IEnumerable<T> source) { return new ObjectShredder<T>().Shred(source, null, null); } here it is showing "Extension method must defined in a top level static class"
-
Sergey Berezovskiy almost 12 yearsSeems like you declared
CustomLINQtoDataSetMethods
class inside some other class. Move it to the top level, directly in some namespace. -
steve almost 12 yearshi, I used copy to datatable method to convert the linq query to datatable and when i debug the application,it is throwing "NotsupportedException handled by user code" and "dataset doesnot support system.data.nullable" at the second foreach loop in Datatable Extendable method().
-
Sergey Berezovskiy almost 12 yearsAlso you can verify if property type is nullable
type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Nullable<>))
and provide nullable parameter type insteadp.PropertyType.GetGenericArguments()[0]