【发布时间】:2020-08-14 18:18:17
【问题描述】:
我想使用搜索关键字获取学校校长。学校 可以有一个用户列表,其中一个是主要角色。
现在我的问题是我正在实现一个自动搜索,它需要 关键字并根据学校名称、代码和校长进行搜索 名字。
代码:
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();
}
错误:
LINQ 表达式 'DbSet .Where(s => s.Name.Contains(__searchKeyword_0) || s.CDSCode.Contains(__searchKeyword_0) || DbSet .Where(u => EF.Property>(s, "Id") != null && EF.Property>(s, "Id") == EF.Property>(u, “学校标识”)) .Where(u => (int)u.DataEntityState == 1 && (int)u.UserDesignation == 1) .Select(u => u.FullName) .FirstOrDefault().Contains(__searchKeyword_0))' 无法翻译。要么以可以翻译的形式重写查询, 或通过插入调用显式切换到客户端评估 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync()。见https://go.microsoft.com/fwlink/?linkid=2101038 更多信息。
【问题讨论】:
标签: c# entity-framework linq entity-framework-core ef-core-3.1