I have two tables: categories and articles. One category can have many articles. I want to display overview of new articles separated by categories. But I want to display only first five articles per each category. How should I write query to solve this?
My first idea was something like this:
var cat = from c in ctx.categories
where c.IsPublic == true
select c;
But this contains all articles and I’m not sure how to write something like c.articles.Take(5) to query. Is it possible?
Oh, and it is ASP.NET MVC 2 with Entity Framework.
This worked for me. Important here is to switch off lazy loading to avoid that all articles are loaded when you iterate through the collection:
Edit
If you don’t dispose
ctxand work with the context more after the code snippet above you probably better reenable lazy loading again …… before you get into trouble with other queries if your application mostly relies on lazy loading.