简体   繁体   中英

Selecting some field after using distinct in Linq

I want to use distinct in linq. After i use disctinct i don't select any field. Is it possible to select after distinct.

  query.select(x=>x.FirmName).Distinct().Select(x => new InvoiceSumReportrModel { Firma = x.FirmName, Id = x.Id,Country=x.Country }).AsQueryable();

Instead of using Distinct , you can use GroupBy to create a group for each FirmName, then grab the first firm from each group and project it to an InvoiceSumReportModel ...

query.GroupBy(x => x.FirmName,
              (k, g) => g.Select(
                x => new InvoiceSumReportrModel 
                { 
                    Firma = x.FirmName, 
                    Id = x.Id,
                    Country = x.Country 
                })
                .First());

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