简体   繁体   English

实体框架Linq查询:如何在多个Nav属性中定位并从第3个Nav属性中进行选择

[英]Entity Framework Linq Query: How to Where on Multiple Nav Properties and Select from 3rd Nav Property

I have the following models with nav propertiers set up in Entity Framework Core: 我在实体框架核心中设置了以下具有导航属性的模型:

  • CRMLocations (one-to-many) CRMPeoples CRMLocations(一对多)CRMPeoples
  • CRMPeoples (one-to-many) CRMEmails CRMPeoples(一对多)CRMEmails
  • CRMPeoples (one-to-many) CRMPhones CRMPeoples(一对多)CRMPhones

I have the following working Query: 我有以下工作查询:

        var iqable = _crmDbContext.CRMPeoples
            .Where(p =>
                p.Name.ToLower().Contains(query) ||
                (from e in p.CRMEmails where e.EmailAddress.ToLower().Contains(query) select e).Any() ||
                (from h in p.CRMPhones where h.PhoneNumberNormalized.Contains(query) select h).Any())
            .Select(p => new CRMSearchResultDTO()
            {
                PersonName = p.Name,
                LocationName = p.CRMLocations.Name,
            });

How can I replace the "(from in where select).Any()" statements to use Linq's lambda syntax? 如何替换“(from where select).Any()”语句以使用Linq的lambda语法? Must result in 1 SQL statement. 必须产生1条SQL语句。 Can use left outter joins, or nested select. 可以使用左外部联接或嵌套选择。

var iqable = _crmDbContext.CRMPeoples
        .Where(p =>
            p.Name.ToLower().Contains(query) ||
            p.CRMEmails.Where(e => e.EmailAddress.ToLower().Contains(query)).Any() ||
            p.CRMPhones.Where(h => h.PhoneNumberNormalized.Contains(query)).Any())
        .Select(p => new CRMSearchResultDTO()
        {
            PersonName = p.Name,
            LocationName = p.CRMLocations.Name,
        });

I got this code by using ReSharper's command "Convert LINQ to method chain" 我通过使用ReSharper的命令“将LINQ转换为方法链”获得了此代码

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

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