I’m having a bit of a problem trying to refine/refactor a LINQ to Entities query. Here’s what I currently have:
public List<Article> LastFive()
{
return _siteDB.Articles.OrderByDescending(a => a.LastModified).Take(5).ToList();
}
What I’d like to do is, instead of returning a list of the five most recent Articles, I’d like to simply get the Title and ID of the five most recent articles. I’d like to keep the method chaining intact, if possible (I like it from a style standpoint), but I’m not sure what to put for Select() using method chaining. I’m also not sure what to put as my method’s return value since I won’t be returning full Article entities, but only selected properties from them. List<IQueryable>?
It’s better to create a typed class:
then:
or, since you are using C# 4.0, you can choose what properties to return:
To use it: