I'm new to Linq so I appologise for the long question.
I have a CMS with articles, categories and versioning. Categories are hierarchical (i.e. Category has a ParentID) and we're using it in a slightly complicated way.
The data structure is as follows:
Category 1 with all its sub-categories (and sub-sub-categories) are considered Domains.
Category 2 with all its sub-categories are considered Knowledge Types.
Articles will be created and associated with 'n+' Domains and sub-(sub)-domains as well as 'n+' Knowledge Types.
Example:
Article Title - "Youth Development"
Domain - Development
Sub-Domain - Youth
Sub-sub-Domain - minors
Knowledge Type - Best Practice
What I need to do:
1. First extract a list of the latest version articles filtered by a given Domain
2. Second, return a list of articles grouped by Knowledge type
Because of the complexity of the table structures, i.e. multiple tables for articles (and versions), categories and article to category (versions) I'm finding it difficult to come up with a single linq query to do this.
Here's a query to get just the latest version articles:
var articleGroups = from article in _Context.Articles
join articleVersion in _Context.ArticleVersions
on article.ArticleID equals articleVersion.ArticleID
join articleCategoryVersion in _Context.ArticlesCategoriesVersions
on articleVersion.ArticleID equals articleCategoryVersion.ArticleID
where articleCategoryVersion.CategoryID == 36
join articleCategory in _Context.ArticleCategories
on articleCategoryVersion.CategoryID equals articleCategory.CategoryID
group articleVersion by article.ArticleID into articleGroup
select articleGroup.OrderByDescending(x => x.Version).First() into articleOut
select new
{
ArticleID = articleOut.ArticleID,
ArticleVersion = articleOut.Version,
Title = articleOut.Title
};
I get 1 sql query (looking at profiler), which is great! I get the articles associated with Domain 36, good.
The query looks convoluted though?
Now I just somehow need to take the resulting articles, join them with knowledge types and group them so that I can display a list of articles on a website grouped by knowledge type.
Article.ArticleVersions
. – Gert Arnold