我对LINQ缺乏了解 [英] My lack of understanding LINQ
问题描述
好,我有几个链接在一起的表.
Ok I have several tables linked together.
Departments (k = departmentID)
Functions (k = functionID, fk = departmentID)
Processes (k = processID, fk = functionID)
Procedures (k = procedureID, fk = processID)
因此,当我想出一些我遇到一些奇怪的问题时,他们都会建立自己的关系.
So they all have their relationships setup when trying to come up with some linq I run into some oddities.
下面的代码将返回我
Departments.Select(s => s.Functions)
但是,当尝试进一步扩展该查询时,它不会让我接受.我希望能够加入上述所有表格,并根据需要从中提取信息.
But when trying to expand that query further it will not let me. I'd like to be able to join all of the above tables and pull information out of them as I need it.
Departments.Select(s => s.Functions.Process.Procedure) // Errors out
我还可以执行以下操作:
Further more I can do the following:
Functions.Select(s => s.Processes)
似乎它将对两个表执行此操作,但不超过2个?我想念什么吗?
It seems it will do it for two tables but no more than 2? Am I missing something?
推荐答案
这就是问题. Departments
与Functions
的关系是一对多的.因此,当您撰写时,
Here's the thing. The Departments
to Functions
relationship is one to many. So when you just write,
Departments
您有一个Department
个对象的集合.由于每个Department
对象都有其自己的Function
对象集合,因此请执行以下操作:
you have a collection of Department
objects. Since each Department
object has its own collection of Function
objects, doing:
Departments.Select(departmentObject => departmentObject.Functions)
为您提供了Function
个对象的集合.
gives you a collection of collections of Function
objects.
如果要将所有这些汇总在一起,则必须使用其他方法,特别是
If you want to aggregate all these together, you have to use a different method, specifically
Departments.SelectMany(departmentObject => departmentObject.Functions)
上面写着获取Function
个对象的集合并使其成为一个大的Function
个对象的集合"
which says "get the collection of collections of Function
objects and make them into one big collection of Function
objects"
您想做的是:
Departments.Select(departmentObject => departmentObject .Functions.Process.Procedure)
但是,这可能行不通,因为您要向Function
对象的集合请求其Process
属性.但是,Function
对象的集合没有Process
属性. Function
对象本身具有Process
属性.因此,您真正想做的是:
But, this can't possibly work, because you are asking a collection of Function
objects for its Process
property. But, a collection of Function
objects doesn't have a Process
property. A Function
object, itself, has a Process
property. So, what you are really trying to do is:
Departments
.SelectMany(departmentObject => departmentObject.Functions)
.Select(functionObject => functionObject.Process.Procedure)
基本上翻译为获取Function
对象的集合的集合,并使它们成为Function
对象的一个大集合.然后,获取每个Function
的Process
属性的Procedure
属性.目的".
which basically translates to "get the collection of collections of Function
objects and make them into one big collection of Function
objects. Then, get the Procedure
property of the Process
property of each Function
object".
因此,在这里您应该期望的只是与任何部门相关的任何功能的过程所执行的过程的集合.
So, what you should be expecting here is just a collection of the procedures that are performed by the processes of any function associated with any of the departments.
请注意,如果部门职能,职能流程或流程过程中存在任何重叠,那么最终结果中可能会出现一些重复的过程.如果这不是您想要的,则可以使用Distinct()
方法删除重复项,并且应该在有重叠但没有重复的任何时候这样做.您可以通过在查询末尾添加Distinct()
来获得相同的结果,但是如果您一路杀掉重复项,将会有更好的性能.因此,实际上,您的查询可能看起来像在下一个查询和上一个查询之间,具体取决于您有多少重叠和您想要多少重复:
Note, that if there is any overlap in the Functions of Departments, in the Processes of Functions, or in the Procedures of Processes, then you may get some duplicate Procedures in your end result. If this isn't what you want, then you can use the Distinct()
method to remove duplicates, and you should do so at any point where there is overlap, but there should not be duplication. You can get the same result by simply adding Distinct()
to the end of the query, but you will have better performance if you kill the duplicates along the way. So, in reality, your query will probably look like something in between the following and previous query, depending on how much overlap there is and how much duplication you want:
Departments
.SelectMany(departmentObject => departmentObject.Functions)
.Distinct()
.Select(functionObject => functionObject.Process)
.Distinct()
.Select(processObject => processObject.Procedure)
.Distinct();
这篇关于我对LINQ缺乏了解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!