Domanda

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();
È stato utile?

Soluzione

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();

Altri suggerimenti

You can try this:

var result = Context.Objects.Where(p => p.ObjectId == ObjectId).OrderByDescending(x => x.DateOrdered).FirstOrDefault();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top