我正在尝试执行下面的Linq查询,但问题是当我使用搜索查询文本在Linq查询下执行时,我一直在获取sql超时。请建议更好的方法来优化这一点。我的Db表有近50万条记录。
{
QueryBuilder = (from ub in idamDbContext.UserBasicInfo
join ur in idamDbContext.UserRoles on ub.UserId equals ur.UserBasicInfoId
join r in idamDbContext.Role on ur.RoleId equals r.Id
join urt in idamDbContext.UserType on r.UserTypeId equals urt.Id
where (r.UserTypeId == 1 || r.UserTypeId == 3 || r.UserTypeId == 2) &&
(territoryId == null || ub.TerritoryId == territoryId) &&
(centerId == null || ub.CenterId == centerId) &&
(isActive == null || ub.IsActive == isActive)
orderby ub.Created
select new UserProfileDTO
{
UserId = ub.UserId,
UserName = ub.UserName,
FirstName = ub.FirstName,
LastName = ub.LastName,
Email = ub.Email,
TerritoryId = ub.TerritoryId,
UserTypeId = r.UserTypeId,
Description = r.Description,
Name = urt.Name
}
);
if (!string.IsNullOrEmpty(query))
QueryBuilder = QueryBuilder.Where(x => (!string.IsNullOrEmpty(x.FirstName) && x.FirstName.ToLower().Contains(query)) ||
(!string.IsNullOrEmpty(x.LastName) && x.LastName.ToLower().Contains(query)) ||
(!string.IsNullOrEmpty(x.Email) && x.Email.ToLower().Contains(query)) ||
(!string.IsNullOrEmpty(x.Description) && x.Description.ToLower().Contains(query)) ||
(!string.IsNullOrEmpty(x.UserName) && x.UserName.ToLower().Contains(query)) ||
(!string.IsNullOrEmpty(x.Name) && x.Name.ToLower().Contains(query))
);
}