LINQ,如何指定包含 List<> 中的项目

发布于 2024-10-29 04:43:20 字数 657 浏览 1 评论 0 原文

好的,我试图从我的数据库中找到属于我提供的类别的所有子类别(使用查询结果整形)。我的类 SubCategory 包括一个 List<>类别。

问题在于,在 linq 语句中,g 指的是 SubCategory(最终包含 Category<>)。所以下面的说法是不允许的。

如何更改 Linq 语句以生成正确的 SQL 查询以包含包含匹配类别的所有子类别。

 public class SubCategory

    {
        public int SubCategoryId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public List<Article> Articles { get; set; }
        public List<Category> Categories { get; set; }
    }

//incorrect code below:
var SubCategories = storeDB.SubCategories.Include("Categories").Single(g => g.Name == category);

Ok, I am trying to find get all subcategories from my database (using query result shaping) that belong to the category that I supply. My class SubCategory includes a List<> of Categories.

The problem is that in the linq statement, g is referring to SubCategory (which in the end contains Categories<>). So the statement below is not allowed.

How do I change the Linq statement to generate the correct SQL query to include all SubCategories that contain the matching Category.

 public class SubCategory

    {
        public int SubCategoryId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public List<Article> Articles { get; set; }
        public List<Category> Categories { get; set; }
    }

//incorrect code below:
var SubCategories = storeDB.SubCategories.Include("Categories").Single(g => g.Name == category);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枯寂 2024-11-05 04:43:20

这对我有用(也许太简单了):

var Category = storeDB.Categories.Include("SubCategories").Single(c => c.Name == category);
return Category.SubCategories;

This worked for me (maybe too simple):

var Category = storeDB.Categories.Include("SubCategories").Single(c => c.Name == category);
return Category.SubCategories;
紫罗兰の梦幻 2024-11-05 04:43:20

我觉得有点令人困惑的是,每个子类别可以属于多个类别 - 您是否正确地理解了这种关系?

无论如何,我认为如果您首先将选择更改为在类别上工作,那么它可能更具可读性 - 即:

var subCatQuery   = from cat in storeDB.Categories
                    where cat.Name == category
                    select cat.SubCategories;

然后您可以执行它来获取 IEnumerable<>:

var subCategories = subCatQuery.ToList();

我发现它更具可读性/可理解性。


(我还发现这里的查询语法比流畅的风格更容易阅读)

I find it a bit confusing that each SubCategory can belong to more than one Category - have you got this relationship the right way around?

Regardless, I think its probably more readable if you change the select to work on Categories first - i.e. something like:

var subCatQuery   = from cat in storeDB.Categories
                    where cat.Name == category
                    select cat.SubCategories;

which you can then execute to get your IEnumerable<>:

var subCategories = subCatQuery.ToList();

I find that much more readable/understandable.


(I also find the query syntax easier to read here than the fluent style)

妥活 2024-11-05 04:43:20

我的首选答案是使用 linq join。如果只有 2 个数据集,您可以使用 linq 操作 .join.groupjoin 与 from 语句。这就是我的做法。

    Dictionary<MainQuery, List<subQuery>> Query = 
    dbStore.List<MainQuery>.include("IncludeAllObjects")
    .groupjoin(db.List<SubTable>.Include("moreSubQueryTables"), 
    mainQueryA=>mainQueryA.PropToJoinOn,
    subQueryB => sunQueryB.PropToJoinOn,
    ((main, sub)=> new {main, sub})
    .ToDictionary(x=>x.main, x=>x.sub.ToList());

my preferred answer would be to use a linq join. if there aren't but 2 data sets you can use the linq operations .join and .groupjoin vs a from statement. this would be my approach.

    Dictionary<MainQuery, List<subQuery>> Query = 
    dbStore.List<MainQuery>.include("IncludeAllObjects")
    .groupjoin(db.List<SubTable>.Include("moreSubQueryTables"), 
    mainQueryA=>mainQueryA.PropToJoinOn,
    subQueryB => sunQueryB.PropToJoinOn,
    ((main, sub)=> new {main, sub})
    .ToDictionary(x=>x.main, x=>x.sub.ToList());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文