简体   繁体   中英

Linq statement created in vb.net not working in c#

I am trying to convert the following in C# and can't figure it out.

Dim query = From p In products Select p.ProductId, p.ProductName, p.ProductCategory Distinct

Is this trying to return an anonymous object?

Here is my attempt in C#:

var query = from p in products select p.ProductId, p.ProductName, p.ProductCategory distinct;

The error I get is: Implicitly typed local variables cannot have multiple declarators.

You have to call the Distinct method, over an the implementation of IEnumerable<T> . It is not part of query expression syntax. Try this:

var query = (from p in products 
            select new { 
                      p.ProductId, 
                      p.ProductName, 
                      p.ProductCategory 
             }).Distinct();

PS: I did not test it to see if it works.

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