实体框架Linq查询:如何在多个导航属性上从何处选择并从第三个导航属性中选择

问题描述 投票:0回答:1

我有以下模型,在Entity Framework Core中设置了nav propertiers:

  • CRMLocations(一对多)CRMPeoples
  • CRMPeoples(一对多)CRME邮件
  • CRMPeoples(一对多)CRMPhones

我有以下工作查询:

        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,
            });

如何替换“(从where where select).Any()”语句来使用Linq的lambda语法?必须导致1个SQL语句。可以使用左外连接或嵌套选择。

c# linq linq-to-sql entity-framework-core
1个回答
1
投票
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,
        });

我使用ReSharper的命令“将LINQ转换为方法链”获得此代码

© www.soinside.com 2019 - 2024. All rights reserved.