1

I have problem with thinking up LINQ query for getting only newest items.
Example of data what I have (left) and what I need to select (right):

Name  Notes DateRecorded  Name  Notes DateRecorded
Item1 A     1.1.2015      Item1 C     1.3.2015
Item1 B     1.2.2015      Item2 X     1.2.2015
Item1 C     1.3.2015
Item2 X     1.2.2015  

Class Properties
  Name As String
  Notes As String
  DateRecorded As Date
End Class
3
  • 1
    What do you mean by "newest" items ? Is it the items with a date recorded <= X days ? Or the X last items on your list ? Commented Oct 6, 2015 at 8:12
  • 1
    I would sort the collection based on the date and then grab the N first elements based on what you need. Commented Oct 6, 2015 at 8:14
  • By newest I mean items, that have latest DateRecorded column. So 1.3.2015 is newest for Item1. Commented Oct 6, 2015 at 8:42

1 Answer 1

3

So you want to group by Name and select the newest by DateRecorded:

Dim newestProps = props.GroupBy(Function(p) p.Name).
    Select(Function(grp) grp.OrderByDescending(Function(p) p.DateRecorded).First())

In query syntax:

newestProps = From p In props
              Order By p.DateRecorded Descending
              Group p By p.Name Into orderedNameGroup = Group
              Select orderedNameGroup.First()
Sign up to request clarification or add additional context in comments.

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.