疯狂查询需要一些反馈

发布于 2024-10-18 10:04:42 字数 416 浏览 2 评论 0原文

        var query =context.Categories.Include("ChildHierarchy")
             .Where(c =>
                 context.CategoryHierarchy.Where(ch => ch.ParentCategoryID == ch.ParentCategoryID)
                 .Select(ch => ch.ChildCategoryID).Contains(c.CategoryID));

问题:

  1. 我需要包含来自另一个导航属性的一些数据(“.Include(“otherprop”)”)
  2. 在完成所有这些之后是否可以执行新的选择?

谢谢

        var query =context.Categories.Include("ChildHierarchy")
             .Where(c =>
                 context.CategoryHierarchy.Where(ch => ch.ParentCategoryID == ch.ParentCategoryID)
                 .Select(ch => ch.ChildCategoryID).Contains(c.CategoryID));

Questions:

  1. I need to include some data from another Navigation Propery (".Include("otherprop")")
  2. Is it possible to do a select new after all of this?

Thanks

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

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

发布评论

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

评论(2

一直在等你来 2024-10-25 10:04:42

你的问题的标题用“疯狂查询”这个词引起了我的兴趣,是的,你是对的,这有点疯狂。

您有一个带有以下谓词的 .Where(...) 子句:

ch => ch.ParentCategoryID == ch.ParentCategoryID

现在这将始终为真。所以我猜你正在尝试做其他事情。我会在答案的最后解释一下这可能是什么。

然后我对您的查询进行了一些清理,以更好地了解您在做什么。这就是它现在的样子:

var query =
    context
        .Categories
        .Where(c => context
            .CategoryHierarchy
            .Select(ch => ch.ChildCategoryID)
            .Contains(c.CategoryID));

因此,我建议不要使用嵌套查询,这样的方法在可读性和性能方面可能会更好:

var query =
    from c in context.Categories
    join h in context.CategoryHierarchy
        on c.CategoryID equals h.ChildCategoryID into ghs
    where ghs.Any()
    select c;

这会给出与您的查询相同的结果,因此希望这会有所帮助。

我确实觉得您正在尝试执行一个查询,其中您想要返回每个类别及其可能具有的任何子类别。如果是这种情况,这里是您需要的查询:

var lookup =
    (from c in context.Categories
     join h in context.CategoryHierarchy
         on c.CategoryID equals h.ChildCategoryID
     select new { ParentCategoryID = h.ParentCategoryID, Category = c, }
    ).ToLookup(x => x.ParentCategoryID, x => x.Category);

var query =
    from c in context.Categories
    select new { Category = c, Children = lookup[c.CategoryID], };

lookup 查询首先对类别和类别层次结构进行联接,以返回所有子类别及其关联的 ParentCategoryID,然后它创建从 ParentCategoryID 到关联 Category 子项列表的查找。

现在,查询只需选择所有类别并在 CategoryID 上执行查找即可获取子项。

使用 .ToLookup(...) 方法的优点是它可以轻松地允许您包含没有子项的类别。与使用 Dictionary<,> 不同,当您使用没有值的键时,查找不会引发异常 - 相反,它会返回一个空列表。

现在,您也可以重新添加 .Include(...) 调用。

var lookup =
    (from c in context.Categories
        .Include("ChildHierarchy")
        .Include("otherprop")
     join h in context.CategoryHierarchy
        on c.CategoryID equals h.ChildCategoryID
     select new { ParentCategoryID = h.ParentCategoryID, Category = c, }
    ).ToLookup(x => x.ParentCategoryID, x => x.Category);

var query =
    from c in context.Categories
        .Include("ChildHierarchy")
        .Include("otherprop")
    select new { Category = c, Children = lookup[c.CategoryID], };

这就是你所追求的吗?

The title to your question intrigued me with the words "Crazy Query", and yes, you're right, it is a bit crazy.

You have a .Where(...) clause with the following predicate:

ch => ch.ParentCategoryID == ch.ParentCategoryID

Now that's going to always be true. So I guess that you're trying to do something else. I'll have a crack at what that might be at the end of my answer.

I then did some cleaning up of your query to get a better idea of what you're doing. This is what it now looks like:

var query =
    context
        .Categories
        .Where(c => context
            .CategoryHierarchy
            .Select(ch => ch.ChildCategoryID)
            .Contains(c.CategoryID));

So rather than use nested queries I would suggest something like this might be better in terms of readability and possibly performance:

var query =
    from c in context.Categories
    join h in context.CategoryHierarchy
        on c.CategoryID equals h.ChildCategoryID into ghs
    where ghs.Any()
    select c;

This gives the same results as your query so hopefully this is helpful.

I do get the impression that you're trying to do a query where you want to return each Category along with any child categories it may have. If that's the case here are the queries you need:

var lookup =
    (from c in context.Categories
     join h in context.CategoryHierarchy
         on c.CategoryID equals h.ChildCategoryID
     select new { ParentCategoryID = h.ParentCategoryID, Category = c, }
    ).ToLookup(x => x.ParentCategoryID, x => x.Category);

var query =
    from c in context.Categories
    select new { Category = c, Children = lookup[c.CategoryID], };

The lookup query first makes a join on categories and the category hierarchies to return all children categories and their associated ParentCategoryID and then it creates a lookup from ParentCategoryID to a list of associated Category children.

The query now just has to select all categories and perform a lookup on the CategoryID to get the children.

The advantage of using the .ToLookup(...) approach is that it easily allows you to include categories that don't have children. Unlike using a Dictionary<,> the lookup does not throw an exception when you use a key that it hasn't got a value for - instead it returns an empty list.

Now, you can add back in the .Include(...) calls too.

var lookup =
    (from c in context.Categories
        .Include("ChildHierarchy")
        .Include("otherprop")
     join h in context.CategoryHierarchy
        on c.CategoryID equals h.ChildCategoryID
     select new { ParentCategoryID = h.ParentCategoryID, Category = c, }
    ).ToLookup(x => x.ParentCategoryID, x => x.Category);

var query =
    from c in context.Categories
        .Include("ChildHierarchy")
        .Include("otherprop")
    select new { Category = c, Children = lookup[c.CategoryID], };

Is that what you're after?

少女的英雄梦 2024-10-25 10:04:42

1) 然后添加它 - context.Categories.Include("ChildHierarchy").Include("OtherCollection");

2) 绝对可以,是的

var query = context.Categories
            .Include("ChildHierarchy")
            .Include("OtherProp")
            .Where(c => context.CategoryHierarchy.Where(ch => ch.ParentCategoryID == ch.ParentCategoryID)
                .Select(ch => ch.ChildCategoryID).Contains(c.CategoryID))
            .Select(c => new { c.A, c.B, c.etc });

1) Then add it - context.Categories.Include("ChildHierarchy").Include("OtherCollection");

2) Absolutely, yes

var query = context.Categories
            .Include("ChildHierarchy")
            .Include("OtherProp")
            .Where(c => context.CategoryHierarchy.Where(ch => ch.ParentCategoryID == ch.ParentCategoryID)
                .Select(ch => ch.ChildCategoryID).Contains(c.CategoryID))
            .Select(c => new { c.A, c.B, c.etc });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文