Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select single column data from DataTable to list

Tags:

c#

asp.net

I have a datatable, dtCpt which has multiple columns in it. It has a column named CLAIM_NUMBER. I have a list List<long> claimNos; I need all the distinct CLAIM_NUMBER from datatable dtCpt to the list claimNos.

I write a code like this

claimNos = dtCpt.AsEnumerable().Select(s => new { id = s.Field<long>("CLAIM_NUMBER") }).Distinct().ToList();

but it show an error like this

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List'

Is there any easy way to do this in a single line of code?

like image 936
Sharon Avatar asked Dec 01 '22 19:12

Sharon


1 Answers

You don't need anonymous type at all. Try that:

claimNos = dtCpt.AsEnumerable()
                .Select(s => s.Field<long>("CLAIM_NUMBER"))
                .Distinct()
                .ToList();
like image 105
MarcinJuraszek Avatar answered Dec 05 '22 01:12

MarcinJuraszek