Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Remove all Users where UserId is in the list

Tags:

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);
    }

So here is my code, I want to filter the result, if the user UserId is in the assetOwnerIdList_ list then remove them eg Result.RemoveAll.Where(user.UserId in assetOwnerIdList_)

like image 989
simpleProgrammer Avatar asked Jan 06 '23 00:01

simpleProgrammer


1 Answers

Simple:

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

If you want to filter useMatches directly in your query, then:

This will only work if you query provider supports the contains method (IN Translation), otherwise, use the method above.

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();
like image 142
Zein Makki Avatar answered Jan 14 '23 22:01

Zein Makki