1

i currently have mutliple records for each "FileStreamID" but i only want to get one distinct one based on the latest "request date"

here is what i have now:

 var resultx = from v in ctx.vEmailSents
                             where v.Lab_ID == 44
                                 && v.UploadDate >= this.BeginDate
                             select new
                             {

                                 FileStreamID = v.FileStream_ID,
                                 UploadDate = v.UploadDate,
                                 RequestDate = v.DateSent,
                                 TypeDesc = v.TypeDesc
                             };

and now i get about 33 records (but i should only get 12)

how can i change it to only give me distinct rows on filestream IDs?

2 Answers 2

4

Group your vEmailSent objects by FileStream_ID and select from each group item with latest date:

 var resultx = from v in ctx.vEmailSents
               where v.Lab_ID == 44
                     && v.UploadDate >= this.BeginDate
               group v by v.FileStream_ID into g
               select g.OrderByDescending(x => x.DateSent)
                       .FirstOrDefault() into lastV
               select new {
                      FileStreamID = lastV.FileStream_ID,
                      UploadDate = lastV.UploadDate,
                      RequestDate = lastV.DateSent,
                      TypeDesc = lastV.TypeDesc                          
               };
Sign up to request clarification or add additional context in comments.

2 Comments

thank you!! i think that did it! all hail to the all mighty LINQ god, @lazyberezovsky!
BTW, the post that was deleted, works if the select new {x ...} contains only the fields to be compared. Often .Distinct() is the better the solution
0
var results = resultx
    .GroupBy(x => x.FileStreamID)
    .Select(g => g.OrderByDescending(x => x.RequestDate).First());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.