【发布时间】:2014-12-20 13:41:52
【问题描述】:
在我的项目中,我在 OrchardProject 网站上使用 this tutorial 实现了记录之间的 N 对 N 关系。我有 2 个部分:MaterialPart & CategoryPart 和关联记录。
材质部分
public class MaterialPartRecord : ContentPartRecord {
public MaterialPartRecord() {
Categories = new List<ContentMaterialCategoryRecord>();
}
}
public class MaterialPart : ContentPart<MaterialPartRecord> {
public IEnumerable<CategoryPartRecord> Categories {
get { return Record.Categories.Select(cmcr => cmcr.CategoryPartRecord); }
}
}
CategoryPartRecord
public class CategoryPartRecord : ContentPartRecord {
...
}
public class CategoryPart : ContentPart<CategoryPartRecord> {
...
}
关联记录:
public class ContentMaterialCategoryRecord {
public virtual int Id { get; set; }
public virtual MaterialPartRecord MaterialPartRecord { get; set; }
public virtual CategoryPartRecord CategoryPartRecord { get; set; }
}
现在我需要选择链接到某个类别的MaterialItems。到目前为止,我有这种方法来提取它们。它有效,但我不确定这是正确的方法。
public IEnumerable<MaterialPart> GetMaterialsByCategory(int catId) {
var cs = new CategoriesService(_oServices);
CategoryPartRecord cat = cs.GetItem(catId).Record;
return _oServices.ContentManager
.Query(VersionOptions.Latest, _contentType)
.Join<CommonPartRecord>()
.OrderByDescending(cpr => cpr.PublishedUtc);
.List()
.Where(ci => ci.IsPublished())
.Select(ci => ci.As<MaterialPart>())
.Where(mp => mp.Categories.Contains(cat)); // < ---- ?
}
所以我的问题是:为所需类别选择 materials 的正确方法是什么,这会产生最佳 SQL 查询,因为我们只需要将 inner join 关联记录表与所需的 CategoryPartRecord_Id 字段值联系起来。
谢谢!
【问题讨论】:
-
您能解释一下为什么不使用分类法吗?
-
@BertrandLeRoy 我已经在上一个关于面包屑的问题中解释过 :) 你总是说分类法,因为它是所有任务的灵丹妙药。
-
好吧,如果我不知道谁在我回答的所有问题上说了些什么,请原谅我,尤其是在没有链接的情况下。当有明确的用例时,我总是会提出分类法。您正在重新实现类别,这正是分类法的设计目的。那你为什么不使用它们呢?
标签: c# nhibernate orchardcms orchardcms-1.8