Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert IQueryable to DataTable

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 to System.Data.DataRow.

var query = from i in dbContext.Personaldetails
            where i.ID == 1
            select i;

            return query.CopyToDataTable();

Any suggestions?

like image 241
steve Avatar asked May 16 '12 12:05

steve


1 Answers

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);
    }
}
like image 139
Sergey Berezovskiy Avatar answered Sep 20 '22 19:09

Sergey Berezovskiy