Darkmage
Darkmage

Reputation: 1603

Grab a List item based on newest datetime

Im grabing articles with this code.

List<view_M04FrontpagesEntity> fList = new view_M04FrontpagesService().GetByCategoryId(0);

var article = new M02ArticlesService().GetById(fList.First<view_M04FrontpagesEntity>().M02ArticleId);

I want to grab the article with the newest article.UpdatedDate how can a best do this with linq or other method?

Upvotes: 3

Views: 133

Answers (5)

Darkmage
Darkmage

Reputation: 1603

This is working as intended.

var m04FrontpagesEntities = new view_M04FrontpagesService().GetByCategoryId(0).ToList().Select(x => new M02ArticlesService().GetById(x.M02ArticleId)).ToList().OrderByDescending(x => x.UpdatedDate); ;
var article = m04FrontpagesEntities.First();

Upvotes: 0

Alex
Alex

Reputation: 8937

var result = (from article in fList
        orderby article.UpdatedDate descending
        select article).First();

Upvotes: 1

nvoigt
nvoigt

Reputation: 77294

var first = articles.OrderByDescending( a => a.UpdateDate ).First();

Generally speaking, this is it. You need to transfer this to your code yourself, because none of the code you posted helped.

Upvotes: 1

Ravi Gadag
Ravi Gadag

Reputation: 15861

you can Use OrderByDescending in LInQ.

var query = myList.Where(x =>x=="somePredicate")
                  .OrderByDescending(x => x.UpdatedDate ).FirstOrDefault();

Returns the first element of a sequence, or a default value if the sequence contains no elements.

Upvotes: 4

Ilya Ivanov
Ilya Ivanov

Reputation: 23626

Use MaxBy method provided my MoreLinq

List<view_M04FrontpagesEntity> fList = new view_M04FrontpagesService().GetByCategoryId(0);

var newest = fList.MaxBy(article => article.UpdatedDate);

Upvotes: 4

Related Questions