LINQ删除UserId在列表中的所有用户

时间:2016-05-30 05:27:09

标签: c# linq

 public JsonResult SearchUser(string term_, int[] assetOwnerIdList_)
    {
        term_ = term_.Trim();

        var userMatches = (from user in _db.Query<User>()
                           where user.Name.Contains(term_) || user.Email.Contains(term_)
                           select new ItemLabelId { Label = user.Email, Id = user.UserId }).ToList().Any();

        return Json(userMatches, JsonRequestBehavior.AllowGet);
    }

所以这是我的代码,我想过滤结果,如果用户UserId在assetOwnerIdList_列表中,则删除它们,例如Result.RemoveAll.Where(user.UserId in assetOwnerIdList_)

由于

2 个答案:

答案 0 :(得分:6)

简单:

Result.RemoveAll(x => assetOwnerIdList_.Contains(x.UserId)); 

如果您想直接在查询中过滤useMatches,那么:

这仅在查询提供程序支持contains方法(IN Translation)时才有效,否则请使用上述方法。

var userMatches = (from user in _db.Query<User>()
                    where user.Name.Contains(term_) || user.Email.Contains(term_)
                    && assetOwnerIdList_.Contains(user.UserId) == false
                    select new ItemLabelId { Label = user.Email, Id = user.UserId }).ToList().Any();

答案 1 :(得分:3)

无需检查用户ID是否在列表中,只需使用这样的过滤器:

var UserIdList = assetOwnerIdList_.ToList();

var list = userMatches .Where(x => !UserIdList.Contains(x.UserId));

return list;