我正在建立一個門戶網站(這是我的愛好項目),併爲我的索引(家)頁我需要寫具體的查詢。Linq到SQL,複雜的選擇
我想按指定順序獲取類別。對於每個類別,我想加載N個最近附加的文章。所以基本上我的主頁將看起來像
Category 1 Category 2
Article title 1 Article title 1
Article title 2 Article title 2
.... ....
Article title N Article title N
Category 3 Category 4
Article title 1 Article title 1
Article title 2 Article title 2
.... ....
Article title N Article title N
我簡化領域模型是這樣的:
public class CategoryInfo
{
public virtual Int64 ParentId? { get; set; }
public virtual String Name { get; set; }
public virtual Int32 DisplayOrder { get; set; }
public virtual ICollection<ArticleInfo> Articles { get; set; }
}
public class ArticleInfo
{
public virtual String Title { get; set; }
public virtual DateTime Published { get; set; }
public virtual Int64? CategoryId { get; set; }
public virtual CategoryInfo { get; set; }
}
類別可以有與之相關的許多文章。文章可以未分類。
所以我試圖做這種方式:
var query = _categoryService.GetCategories().Where(x => !x.ParentId.HasValue) // Getting only top level categories
.OrderBy(x => x.DisplayOrder) // Sorting
.Select(x => x.Articles.OrderByDesc(a => a.Published).Take(N))
這是行不通的。有任何想法嗎 ?
GroupBy運營商的目的是什麼? – Auk 2011-12-16 11:38:32