简体   繁体   English

我对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. 因此,当他们想出一些我遇到一些奇怪的问题时,他们都会建立关系。

The below code will return me 下面的代码将返回我

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)

It seems it will do it for two tables but no more than 2? 看来它将对两个表执行此操作,但不超过2个? Am I missing something? 我想念什么吗?

Here's the thing. 就是这个 The Departments to Functions relationship is one to many. DepartmentsFunctions关系是一对多的。 So when you just write, 所以当你写的时候

 Departments

you have a collection of Department objects. 您有一个Department对象的集合。 Since each Department object has its own collection of Function objects, doing: 由于每个Department对象都有其自己的Function对象集合,因此请执行以下操作:

 Departments.Select(departmentObject => departmentObject.Functions)

gives you a collection of collections of Function objects. 给您一个Function对象集合的集合。

If you want to aggregate all these together, you have to use a different method, specifically 如果要将所有这些汇总在一起,则必须使用其他方法,特别是

 Departments.SelectMany(departmentObject => departmentObject.Functions)

which says "get the collection of collections of Function objects and make them into one big collection of Function objects" 上面写着“获取Function对象的集合并使其成为Function对象的一个​​大集合”

What you are trying to do is: 您正在尝试做的是:

Departments.Select(departmentObject => departmentObject .Functions.Process.Procedure)

But, this can't possibly work, because you are asking a collection of Function objects for its Process property. 但是,这可能行不通,因为您要向Function对象的集合要求其Process属性。 But, a collection of Function objects doesn't have a Process property. 但是, Function对象的集合没有Process属性。 A Function object, itself, has a Process property. Function对象本身具有Process属性。 So, what you are really trying to do is: 因此,您真正想做的是:

 Departments
     .SelectMany(departmentObject => departmentObject.Functions)
     .Select(functionObject => functionObject.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". 它基本上翻译为“获取Function对象的集合并将它们变成一个Function对象的大集合。然后,获取每个Function对象的Process属性的Procedure属性”。

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. 因此,您应该在这里期望的只是与任何部门相关的任何功能的过程所执行的过程的集合。

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. 如果这不是您想要的,则可以使用Distinct()方法删除重复项,并且应该在有重叠但没有重复的任何时候这样做。 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. 您只需在查询末尾添加Distinct()就可以得到相同的结果,但是如果您一路杀掉重复项,则性能会更好。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM