Question

I have a list of objects with Same ObjectId and I want to return the one with latest DateOrdered. Is this a good way to doing it?

var LatestObject = (from o in Context.Objects 
    where p.ObjectId == ObjectId 
    select p.DateOrdered)
    .ToList()
    .Max();
Was it helpful?

Solution

No, it will only display latest date.Not your object, instead you can try:

Context.Objects.Where(p => p.ObjectId == ObjectId)
       .OrderByDescending(p => p.DateOrdered)
       .First();

OTHER TIPS

You can try this:

var result = Context.Objects.Where(p => p.ObjectId == ObjectId).OrderByDescending(x => x.DateOrdered).FirstOrDefault();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top