C# Linq通过搜索关键字从对象的关系列表中选择对象

C# Linq通过搜索关键字从对象的关系列表中选择对象,c#,entity-framework,linq,entity-framework-core,ef-core-3.1,C#,Entity Framework,Linq,Entity Framework Core,Ef Core 3.1,我想用搜索关键字找到学校校长。学校 可以有一个主要角色的用户列表 现在我的问题是我正在实现一个自动搜索,它需要 关键字,并根据学校名称、代码和校长进行搜索 名字 代码: public class School : AuditableDataEntity<int> { [Required] [MaxLength(200)] public string Name { get; set; } [MaxLength(

我想用搜索关键字找到学校校长。学校 可以有一个主要角色的用户列表

现在我的问题是我正在实现一个自动搜索,它需要 关键字,并根据学校名称、代码和校长进行搜索 名字

代码:

 public class School : AuditableDataEntity<int>
    {
        [Required]
        [MaxLength(200)]
        public string Name { get; set; }

        [MaxLength(150)]
        public string Code { get; set; }

        public District District { get; set; }

        public ICollection<UserProfile> Users { get; set; }


    }

public class UserProfile : AuditableDataEntity<Guid>
    {

        [Required]
        [MaxLength(100)]
        public string FirstName { get; set; }

        [Required]
        [MaxLength(100)]
        public string LastName { get; set; }

        [Required]
        [MaxLength(200)]
        public string Email { get; set; }

        [MaxLength(50)]
        public string PhoneWork { get; set; }

        [MaxLength(20)]
        public int PhoneWorkExt { get; set; }

        [MaxLength(50)]
        public string PhoneMobile { get; set; }       

        public UserLevel UserLevel { get; set; }

        public UserRole UserRole { get; set; }

        public UserDesignation UserDesignation { get; set; }

        public School School { get; set; }
        public int? SchoolId { get; set; }

        public string FullName => $"{FirstName} {LastName}";
    }

Task<Response<IEnumerable<SchooSearchDTO>>> ISchoolQueryService.GetSchoolAutoCompleteData(string searchKeyword)
        {
            return _schoolQueryRepository.WithRelatedEntities().Where(x => x.Name.Contains(searchKeyword)
            ||x.Code.Contains(searchKeyword)
            || x.Users.FirstOrDefault(y => y.DataEntityState == DataEntityState.Published && y.UserDesignation == UserDesignation.Principal).FullName.Contains(searchKeyword)).OrderBy(u => u.Name).Select(z => new SchooSearchDTO
            {
                PrincipalName = z.Name,
                CDSCode = z.Code

            }).ToResponseListAsync();
        }
公立学校:AuditableDataEntity
{
[必需]
[MaxLength(200)]
公共字符串名称{get;set;}
[MaxLength(150)]
公共字符串代码{get;set;}
公共区{get;set;}
公共ICollection用户{get;set;}
}
公共类UserProfile:AuditableDataEntity
{
[必需]
[MaxLength(100)]
公共字符串名{get;set;}
[必需]
[MaxLength(100)]
公共字符串LastName{get;set;}
[必需]
[MaxLength(200)]
公共字符串电子邮件{get;set;}
[MaxLength(50)]
公共字符串电话工作{get;set;}
[MaxLength(20)]
public int PhoneWorkExt{get;set;}
[MaxLength(50)]
公共字符串PhoneMobile{get;set;}
公共用户级用户级{get;set;}
公共用户角色{get;set;}
公共用户指定用户指定{get;set;}
公立学校{get;set;}
公共int?SchoolId{get;set;}
公共字符串FullName=>$“{FirstName}{LastName}”;
}
任务ISchoolQueryService.GetSchoolAutoCompleteData(字符串搜索关键字)
{
return _schoolQueryRepository.WithRelatedEntities().Where(x=>x.Name.Contains(searchKeyword)
||x、 Code.Contains(搜索关键字)
||x.Users.FirstOrDefault(y=>y.DataEntityState==DataEntityState.Published&&y.UserDesignation==UserDesignation.Principal).FullName.Contains(searchKeyword)).OrderBy(u=>u.Name).选择(z=>new-SchooSearchDTO
{
PrincipalName=z.名称,
CDSCode=z.代码
}).ToResponseListSync();
}
错误:

LINQ表达式的DbSet 其中(s=>s.Name.Contains(uu searchKeyword_0)| s.CDSCode.Contains(u searchKeyword_0)| DbSet 其中(u=>EF.Property>(s,“Id”)!=null和&EF.Property>(s,“Id”)==EF.Property>(u, “学校ID”)) 其中(u=>(int)u.DataEntityState==1&(int)u.UserDesignation==1) .选择(u=>u.FullName) .FirstOrDefault().Contains(\uuu searchKeyword\u 0))无法翻译。或者以可以翻译的形式重写查询, 或者通过插入对的调用显式切换到客户端评估 AsEnumerable()、AsAsAsAsyncEnumerable()、ToList()或 ToListSync()。寻找 更多信息


问题是EF无法将其转换为SQL

x.Users.FirstOrDefault(y => y.DataEntityState == DataEntityState.Published && y.UserDesignation == UserDesignation.Principal).FullName.Contains(searchKeyword)
Task<Response<IEnumerable<SchooSearchDTO>>> ISchoolQueryService.GetSchoolAutoCompleteData(string searchKeyword)
{
    return _schoolQueryRepository.WithRelatedEntities().Where(x => x.Name.Contains(searchKeyword)
        ||x.Code.Contains(searchKeyword)
        || x.Users.Any(y => y.DataEntityState == DataEntityState.Published && y.UserDesignation == UserDesignation.Principal && y.FirstName.Contains(searchKeyword))
    ).OrderBy(u => u.Name).Select(z => new SchooSearchDTO
    {
        PrincipalName = z.Name,
        CDSCode = z.Code

    }).ToResponseListAsync();
}
因此,尝试使用
Any
,而不是在LINQ中使用
FirstOrDefault
选择用户,以便EF能够将其转换为SQL

x.Users.FirstOrDefault(y => y.DataEntityState == DataEntityState.Published && y.UserDesignation == UserDesignation.Principal).FullName.Contains(searchKeyword)
Task<Response<IEnumerable<SchooSearchDTO>>> ISchoolQueryService.GetSchoolAutoCompleteData(string searchKeyword)
{
    return _schoolQueryRepository.WithRelatedEntities().Where(x => x.Name.Contains(searchKeyword)
        ||x.Code.Contains(searchKeyword)
        || x.Users.Any(y => y.DataEntityState == DataEntityState.Published && y.UserDesignation == UserDesignation.Principal && y.FirstName.Contains(searchKeyword))
    ).OrderBy(u => u.Name).Select(z => new SchooSearchDTO
    {
        PrincipalName = z.Name,
        CDSCode = z.Code

    }).ToResponseListAsync();
}
任务是学校查询服务。GetSchoolAutoCompletedData(字符串搜索关键字)
{
return _schoolQueryRepository.WithRelatedEntities().Where(x=>x.Name.Contains(searchKeyword)
||x、 Code.Contains(搜索关键字)
||x.Users.Any(y=>y.DataEntityState==DataEntityState.Published&&y.UserDesignation==UserDesignation.Principal&&y.FirstName.Contains(搜索关键字))
).OrderBy(u=>u.Name)。选择(z=>newschoosearchdto
{
PrincipalName=z.名称,
CDSCode=z.代码
}).ToResponseListSync();
}
见:

编辑:


您还需要使用
FirstName
,因为
FullName
是SQL中没有的属性。

同样的错误,任何人都需要寻找其他解决方法。更改为&&y.FirstName.Contains(searchKeyword)会起作用。FullName的问题是,所以它通过更改为any并将FullName更改为FirstName来工作。是的,很好的一点,我忘了提那件事。。我已经更新了我的答案。