Madam Zu Zu
Madam Zu Zu

Reputation: 6605

Getting Distinct Values with LINQ

i need to be able to get distinct values grouped by FileID and by SentToID

this is what i have now, and it only groups by SentToID, which is missing some records.

var sentByResults = from v in ctx.vEmailSents
                                            where v.TypeDesc.Equals("Request")
                                             group v by v.SentTo_ID into g
                                            select g.OrderByDescending(x => x.DateSent).FirstOrDefault() into lastV
                                            select new
                                            {
                                                ClaimID = lastV.Claim_ID,
                                                SentToID= lastV.SentTo_ID,

                                            };

so if i have 5 records

claim id    fileid   sentToID
1            15         27
1            16         27
1            15         26
1            15         26
1            15         47

right now i get 3 records back, one for each unique sentToID, but i need to get 4 records back, for each unique ID within each unique fileID

Upvotes: 2

Views: 151

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500525

I suspect you just want to group by an anonymous type:

group v by new { v.SentTo_ID, v.FileID }

Also, given that you'll never get any empty groups, you should be able to use First instead of FirstOrDefault.

Upvotes: 4

Related Questions