简体   繁体   中英

Converting Vb.Net Linq to C# but Distinct Error

I am trying to move some of my code over from vb.net to c# class library. I have linq query in vb.net that is querying against a DataTable.

Dim qryUser = From db In dt.AsEnumerable() _
              Select userName = db.Field(Of String)("UserName") Distinct
              Order By userName Ascending

Now I am trying to make this work in C# and I have

IEnumerable<string> qryUser = (from db in Table.AsEnumerable()
                               orderby db.Field<string>("UserName") ascending
                               select db.Field<string>("UserName"));

Now all examples I have found searching, shows I should just have to add

.Distinct()

to the end of my c# linq query to make it look like this

IEnumerable<string> qryUser = (from db in Table.AsEnumerable()
                               orderby db.Field<string>("UserName") ascending
                               select db.Field<string>("UserName")).Distinct()

but doing this gives me an error in my IDE

Error 1 'System.Data.EnumerableRowCollection' does not contain a definition for 'Distinct' and no extension method 'Distinct' accepting a first argument of type 'System.Data.EnumerableRowCollection' could be found

Can someone, please, show me the proper way of doing this? Thanks in advance.

I was able to copy/paste your code (and setup a small DataTable ) and it worked fine.

The Distinct extension method lives in the System.Linq namespace.

Make sure the following using directive is at the top of your class file:

using System.Linq;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM