即使子表中没有记录,如何返回记录?
以下查询返回员工列表及其姓名、电子邮件地址和各自的公司名称。问题在于我们系统中没有电子邮件地址的员工。如果员工没有电子邮件地址,他们将被完全排除在结果集中。
如何为不存在的电子邮件地址返回 null(或空字符串等),而不是排除该员工?
var employees = from e in Employees where e.ContactAttributes.Any (ca => ca.AttributeID == 19730317 )
from om in e.ChildOrganizationMaps
from ea in e.EmailAddresses
select new {e.FName, e.LName, ea.EmailAddress, om.Parent.CompanyName};
employees.Dump();
The following query returns a list of employees and their names, email address, and respective company names. The problem is with employees that do not have an email address in our system. If an employee doesn't have an email address, they are excluded entirely from the resultset.
How do I return null (or empty string, etc) for an email address that doesn't exist, rather than excluding the employee?
var employees = from e in Employees where e.ContactAttributes.Any (ca => ca.AttributeID == 19730317 )
from om in e.ChildOrganizationMaps
from ea in e.EmailAddresses
select new {e.FName, e.LName, ea.EmailAddress, om.Parent.CompanyName};
employees.Dump();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的查询正在使用联接。您可以使用外部联接,或使用 Include 进行“预加载”,以便提取所有实体及其关系:
Your query is using joins. You can either use an outer join, or use Include for "eager loading" so that you will pull all entities and their relations:
使用外连接。对于表中没有行的字段,它将返回 null。
Use an outer join. It will return null for fields in the table which doesn't have a row.
编辑:
好的,使用显式左连接 (DefaultIfEmpty) 来实现。
Edit:
Ok, do it with explicit left join (DefaultIfEmpty) than.