TheFallenOne
TheFallenOne

Reputation: 1686

To remove records from a list who's column contains elements of the second list using Linq

So I have a list of string with values.

List<string> toBeExcluded = new List<string>() { "gmail.com", "yahoo.com" };

I have a query which gets me a list from the database and one of the column is user email.

var user = context.Users.Where(w => !string.IsNullOrEmpty(w.Email));

Now I need to exclude those users who have emails containing the values in toBeExcluded. So all records with Email of domain gmail.com and yahoo.com needs to be removed.

I tried this, but it did not work.

var toBeSent = user.Where(w => !toBeExcluded.Contains(w.Email));    

So what am i missing?

Upvotes: 0

Views: 314

Answers (1)

Alex - Tin Le
Alex - Tin Le

Reputation: 2000

Try this

var toBeSent = user.Where(w => !toBeExcluded.Any(e => w.Email.EndsWith(e)));    

Upvotes: 1

Related Questions