altandogan
altandogan

Reputation: 1285

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();

Upvotes: 0

Views: 104

Answers (1)

Anthony Chu
Anthony Chu

Reputation: 37520

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());

Upvotes: 2

Related Questions