疯狂查询需要一些反馈
var query =context.Categories.Include("ChildHierarchy")
.Where(c =>
context.CategoryHierarchy.Where(ch => ch.ParentCategoryID == ch.ParentCategoryID)
.Select(ch => ch.ChildCategoryID).Contains(c.CategoryID));
问题:
- 我需要包含来自另一个导航属性的一些数据(“.Include(“otherprop”)”)
- 在完成所有这些之后是否可以执行新的选择?
谢谢
var query =context.Categories.Include("ChildHierarchy")
.Where(c =>
context.CategoryHierarchy.Where(ch => ch.ParentCategoryID == ch.ParentCategoryID)
.Select(ch => ch.ChildCategoryID).Contains(c.CategoryID));
Questions:
- I need to include some data from another Navigation Propery (".Include("otherprop")")
- Is it possible to do a select new after all of this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题的标题用“疯狂查询”这个词引起了我的兴趣,是的,你是对的,这有点疯狂。
您有一个带有以下谓词的
.Where(...)
子句:现在这将始终为真。所以我猜你正在尝试做其他事情。我会在答案的最后解释一下这可能是什么。
然后我对您的查询进行了一些清理,以更好地了解您在做什么。这就是它现在的样子:
因此,我建议不要使用嵌套查询,这样的方法在可读性和性能方面可能会更好:
这会给出与您的查询相同的结果,因此希望这会有所帮助。
我确实觉得您正在尝试执行一个查询,其中您想要返回每个类别及其可能具有的任何子类别。如果是这种情况,这里是您需要的查询:
lookup
查询首先对类别和类别层次结构进行联接,以返回所有子类别及其关联的ParentCategoryID
,然后它创建从ParentCategoryID
到关联Category
子项列表的查找。现在,查询只需选择所有类别并在
CategoryID
上执行查找即可获取子项。使用
.ToLookup(...)
方法的优点是它可以轻松地允许您包含没有子项的类别。与使用Dictionary<,>
不同,当您使用没有值的键时,查找不会引发异常 - 相反,它会返回一个空列表。现在,您也可以重新添加
.Include(...)
调用。这就是你所追求的吗?
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: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:
So rather than use nested queries I would suggest something like this might be better in terms of readability and possibly performance:
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:
The
lookup
query first makes a join on categories and the category hierarchies to return all children categories and their associatedParentCategoryID
and then it creates a lookup fromParentCategoryID
to a list of associatedCategory
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 aDictionary<,>
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.Is that what you're after?
1) 然后添加它 -
context.Categories.Include("ChildHierarchy").Include("OtherCollection");
2) 绝对可以,是的
1) Then add it -
context.Categories.Include("ChildHierarchy").Include("OtherCollection");
2) Absolutely, yes