2

I have a list Having multiple Items and 3 props ID,DATE,COMMENT.ID field is Auto incremented in DATABASE.

Let say list Contains

2,16AUG,CommentMODIFIED
1,15AUG,CommentFIRST
3,18AUG,CommentLASTModified

I want to get a single ITEM.Item Having Minimum DATE and having Latest Comment. In this case

1,15AUG,CommentLASTModified

Any easy way to do it using LINQ.

3
  • 1
    1,15AUG,CommentLASTModified this row is formed from different rows in database. Is that what you want? Commented Aug 22, 2013 at 6:20
  • Basically I want a single record with Minimum dates and latest comment Commented Aug 22, 2013 at 6:22
  • 1,15AUG,CommentFIRST - is a record with minimum date. 3,18AUG,CommentLASTModified is a record with latest comment. You want a combination of these records Commented Aug 22, 2013 at 6:23

2 Answers 2

4
orderedItems = items.OrderBy(x => x.Date);

var result = items.First();
result.Comment = items.Last().Comment;
Sign up to request clarification or add additional context in comments.

Comments

1

To get a single item out of the list, you can order the items then take the first one, like this:

var result = items
    .OrderByDescending(x => x.Date)
    .First();

But First will throw an exception if the items collection is empty. This is a bit safer:

var result = items
    .OrderByDescending(x => x.Date)
    .FirstOrDefault();

To get the min / max of different columns you can do this:

var result = 
    new Item {
         Id = 1,
         Date = items.Min(x => x.Date),
         Comment = items.Max(x => x.Comment)
    };

But this will require two trips to the database. This might be a bit more efficient:

var result = 
    (from x in items
     group x by 1 into g
     select new Item {
         Id = 1,
         Date = g.Min(g => g.Date),
         Comment = g.Max(g => g.Comment)
     })
    .First();

Or in fluent syntax:

var result = items
    .GroupBy(x => 1)
    .Select(g => new Item {
         Id = 1,
         Date = g.Min(g => g.Date),
         Comment = g.Max(g => g.Comment)
     })
    .First();

4 Comments

items.Min(x => x.Comment) this will probably select the wrong comment
@IlyaIvanov You're right, it should be the "latest" comment, which would be .Max(x => x.Comment) (assuming that's actually a date as the question seems to imply)
@p.s.w.g I doubt it is. I read that as "The comment from August 18th." Your code will get the comment in highest alphabetical order, which is essentially random. Still, +1 as your answer is much more thorough.
This database design is a common anti-pattern, where they want to keep the entire comment history (instead of rewriting it) but instead of creating a new table to hold all related comments, just add new records to the system. Spend enough time as a consultant and you learn AAAAAAWLLLL the anti-patterns.

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.