1

I have the following use of Linq from a datatable:

  var query = dt.AsEnumerable();
query = dt.AsEnumerable().Where(log => log.Field<byte>("Day") == day).Take(10);

The following error:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Data.DataRow>' to 'System.Data.EnumerableRowCollection<System.Data.DataRow>'. An explicit conversion exists (are you missing a cast?)

I have tried take(10) - Please can you advise?

Luke Wilkinson
  • 439
  • 8
  • 17
  • Have you posted entire query without any modifications? The same query works for me fine (.NET 4). Are you filled in dataTable manually? if so please show code – sll Dec 12 '11 at 13:45
  • Sorry no the var is declared as var query = dt.AsEnumerable(). If I explicitly declare it as IEnumerable how would I then get query.AsDataView() which is how I am binding it to a ListView? – Luke Wilkinson Dec 12 '11 at 13:49
  • Gotcha, modify the question please – sll Dec 12 '11 at 13:52

1 Answers1

4

Does the error definitely point to that line, on its own? I'd expect you to get that if you tried:

var query = dt.AsEnumerable();
query = query.Where(log => log.Field<byte>("Day") == day).Take(10);

in which case you could fix it by changing the type of query to be explicitly IEnumerable<DataRow>.

(If that's not the problem, please give us more context. A short but complete method demonstrating just the problem at hand would help.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Sorry yes the var is declared as you have put it. If I explicitly declare it as IEnumerable how would I then get query.AsDataView() which is how I am binding it to a ListView? – Luke Wilkinson Dec 12 '11 at 13:45
  • @LukeWilkinson: You'd probably want to copy it back to a DataTable, e.g. with `CopyToDataTable` - http://msdn.microsoft.com/en-us/library/bb396189.aspx – Jon Skeet Dec 12 '11 at 13:48