Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert EnumerableRowCollection<string> to List<string>

Tags:

c#

linq

I am trying to return this collection in a function:

List<string> codes = ( from q in Data.AsEnumerable() select q.Field<string>("START") );
return codes;

This throws error:

Cannot implicitly convert type 'System.Data.EnumerableRowCollection<string>' to 'System.Collections.Generic.List<string>'

What's the best way to return such a collection?

like image 943
Meidi Avatar asked Apr 24 '13 21:04

Meidi


1 Answers

Since EnumerableRowCollection<TRow> implements IEnumerable<T> you can just add a ToList() at the end:

using System.Linq;

List<string> bankCodes = ( from q in QueueData.AsEnumerable() select q.Field<string>("START") ).ToList();

or alternatively

List<string> bankCodes =  QueueData.AsEnumerable()
    .Select(r => r.Field<string>("START"))
    .ToList();
like image 146
Lee Avatar answered Oct 18 '22 18:10

Lee