.SelectMany() 并从多个相关表中获取数据
此查询返回员工 ID、姓名、公司 ID、公司名称和公司城市。我缺少员工电子邮件地址(存储在 EmployeeEmailAddress 表中的 emailAddress)和员工电话号码(存储在 EmployeePhoneNumbers 表中的phoneNumber)。
我需要添加 .SelectMany() 来获取母公司关系并访问公司 ID、名称和城市。但是,现在我无法访问 PersonOrgMap 表中未找到的任何属性。我无法导航到任何其他表格。删除 .SelectMany() 允许我导航到其他表,但我无法访问母公司信息。
var employees = Contacts.Where(c => c.ContactAttributes
.Any (ca => ca.AttributeID == 1153))
.SelectMany (x => x.ChildPersonOrgMaps)
.Select (c => new { employeeId = c.Child.ContactID,
c.Child.FirstName,
c.Child.LastName,
companyId = c.ParentContactID,
c.Parent.OrganizationName,
c.Parent.City
}
)
.OrderBy (c =>c.LastName ).ThenBy(x => x.FirstName)
.Dump();
This query returns employee Id, name, company id, company name, and company city. I'm missing employee email address (emailAddress stored in the EmployeeEmailAddress table) and employee phone numbers (phoneNumber stored in the EmployeePhoneNumbers table).
I needed to add the .SelectMany() to get the parent company relationship and access the company id, name, and city. Now, however, I can't access any properties not found in the PersonOrgMap table. I can't navigate to any other tables. Removing the .SelectMany() allows me to navigate to other tables but I lose access to the parent company information.
var employees = Contacts.Where(c => c.ContactAttributes
.Any (ca => ca.AttributeID == 1153))
.SelectMany (x => x.ChildPersonOrgMaps)
.Select (c => new { employeeId = c.Child.ContactID,
c.Child.FirstName,
c.Child.LastName,
companyId = c.ParentContactID,
c.Parent.OrganizationName,
c.Parent.City
}
)
.OrderBy (c =>c.LastName ).ThenBy(x => x.FirstName)
.Dump();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您热衷于方法语法,那么 SelectMany() 上有一个重载,它还允许您访问“源”和“结果”对象:
If you're keen on the method syntax, then there's an overload on SelectMany() that also gives you access to both the "source" and "result" objects :
这是查询表达式真正有用的地方。如果您像这样启动查询:
稍后您将可以在查询中访问 c 和 om 变量。 C# 通过选择“携带”原始变量的临时匿名类型,将其转换为 SelectMany 调用。查看此内容的最佳方法是在 LINQPad 中将查询编写为查询表达式,然后查看 lambda 选项卡以查看流畅语法的转换。
This is where query expressions really help. If you start the query like this:
you will have access to both the c and om variables later in the query. C# translates this into a SelectMany call by selecting into a temporary anonymous type that "carries" the original variable. The best way to see this is to write the query as a query expression in LINQPad, then look at the lambda tab to see the translation into fluent syntax.
我同意 Joe Albahari 的观点——使用查询语法。这是(据我所知)您可以通过查询完成但无法使用方法语法完成的一件事。
您可以尝试选择包含父对象和子对象的匿名类型,但我认为 EF 不会非常喜欢它。
I agree with Joe Albahari - use query syntax. This is the one thing (that I'm aware of) that you can do with query that you can't with method syntax.
You could try selecting into an anonymous type that contains your parent and child objects, but I don't think that EF will like it very much.
我正在学习 LINQ,我更喜欢拆分命令,这样我就可以知道哪里发生了什么。它当然不如公认的答案有效,但可能可以供任何学习 LINQ 的人使用。
我更喜欢用一些虚拟数据进行演示,
它在 LINQ Pad 中提供输出,如下所示。
在此处输入图片说明
I'm learning LINQ and I prefer to split the commands so I can know what is happening where. It is certainly not efficient as the accepted answer but probably can come to use of anyone who's learning LINQ.
I'll prefer to demonstrate with some dummy data
It gives output in LINQ Pad like this.
enter image description here