//我尝试使用以下查询实现全局搜索
//我的linq查询:

var query = (from ug in db.SC_UserGroup
             where ug.IsDeleted == false && ug.TenantID == tenantId && ug.ParentGroup == defaultGuid && ug.GroupTypeID == manualCodeTypeId
             select new
             {
                 GroupID = ug.ID,
                 GroupTypeID = ug.GroupTypeID,
                 GroupName = ug.GroupName,
                 Description = ug.Description,
                 TenantId = ug.TenantID,
                 SubGroups = (from sug in db.SC_UserGroup
                              where sug.ParentGroup == ug.ID && sug.IsDeleted == false && sug.TenantID == tenantId
                              select new
                              {
                                  GroupID = sug.ID,
                                  GroupTypeID = sug.GroupTypeID,
                                  GroupName = sug.GroupName,
                                  Description = sug.Description,
                                  TenantId = sug.TenantID,
                                  users = (from sugm in db.SC_UserGroupMap
                                           join su in db.SC_User on sugm.UserID equals su.ID
                                           join usertenantmap in db.SC_UserTenantMap on su.ID equals usertenantmap.UserID
                                           join userCredential in db.SC_UserCredential on usertenantmap.UserCredentialID equals userCredential.ID
                                           where su.IsDeleted.Value == false && sugm.UserGroupID == sug.ID && sugm.IsDeleted == false
                                           select new
                                           {
                                               UserId = su.ID,
                                               UserName = (su.FirstName + " " + su.LastName),
                                               EmailId = userCredential.Email
                                           }).Distinct().ToList()
                              })
             });
// here my search array contails the list of words to search
if (searchArray[0] != string.Empty)
{
    //like this i am able to search on GroupName and Description column
    query = query.Where(x => searchArray.All(y => x.GroupName.Contains(y) || x.Description.Contains(y)));
}


我的问题 :
我想搜索子查询字段的字段(即当前上下文中的SubGroups字段)

最佳答案

如果要对SubGroups中的字段进行相同的搜索,则必须使用AllAny,这取决于您是否希望SubGroups中的所有项目都匹配或至少匹配。

query = query.Where(
    x => searchArray.All(
        y => x.GroupName.Contains(y) ||
             x.Description.Contains(y) ||
             x.SubGroups.Any(sg => sg.GroupName.Contains(y) ||
             x.SubGroups.Any(sg => sg.Description.Contains(y))));

关于c# - 如何在嵌套的linq查询字段中搜索一组单词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32947228/

10-13 07:46
查看更多