C # LINQ select where value is not contained in array / list

New to LINQ and not sure of the correct syntax of what I want to do.

I have a Black List, an array, or a list of (maybe either) incorrect codes that I don’t want to put in this new “key list”, which I’m doing

currently...

var keys = (from s in context.Keys where s.Code != "BadCode1" where s.Code != "BadCode2" where s.Code != "BadCode3" where s.Code != "BadCode4" where s.Code != "BadCode5" orderby s.Name select s).ToList<Keys>(); 

how would I cut it to one line and read it from the black list? so more ...

 var keys = (from s in context.Keys where s.Code != ANY OF THE VALUES FROM "BLACKLIST" orderby s.Name select s).ToList<Keys>(); 
+10
source share
8 answers

Add it to the array and then use Contains :

 var badCodes = new[] { "BadCode1", "BadCode2", "BadCode3", "BadCode4", "BadCode5"}; var keys = (from s in context.Keys where !badCodes.Contains(s.Code) orderby s.Name select s).ToList<Keys>(); 
+8
source
 var keys = (from s in context.Keys where !blackList.Contains(s.Code) orderby s.Name select s).ToList(); // do you really need the ToList? 

array or list (can be either)

If you do this in memory (linq for objects), then a HashSet will work better than an array or list. If you do this in a database, it will not make any difference.

Also, do you really need to be listed? If you intend to view the results or otherwise do something for which IEnumerable<> or IQueryable<> will work on its own, you probably should not do this.

+7
source

All other answers are correct, but I personally am a fan of this:

Assuming you have a list of strings called blackList containing all the strings you want to filter.

 var keys = context.Keys.Where(x => !blackList.Contains(x.Code)) .OrderBy(x => x.Name) .ToList(); 

I just love the way this makes my code clear and understandable. But then again, each answer is correct.

+5
source

You can use the .Contains () method to detect the intersection of any collections. Just:

1) define the blacklist:

 var blackList = new List<string> {"BadCode1", "BadCode2", "BadCode3", "BadCode4", "BadCode5"}; 

2) filter the request by crossing with the blacklist (! BlackList.Contains (s.Code)):

 var keys = (from s in context.Keys where !blackList.Contains(s.Code) orderby s.Name select s).ToList<Keys>(); 
+3
source

Create a List<string> with all invalid values

 List<string> sBlackList = new List<string>(){"BadCode1", "BadCode2", "BadCode3"}; 

and replace

 where s.Code != ANY OF THE VALUES FROM "BLACKLIST" 

from

 where !sBlackList.Contains(s.Code) 
+2
source

You can also use the Except Method similar to this:

 var badCodes = new[] { "BadCode1", "BadCode2", "BadCode3", "BadCode4", "BadCode5"}; var blackList = context.Keys.Where(s => badCodes.Contains(s.Code)); var filteredKeys = context.Keys.Except(blackList); 
+2
source

You may have a list of blacklist codes and check if it contains the corresponding codes

 var keys = (from s in context.Keys where !blackList.Contains(s.Code) orderby s.Name select s).ToList<Keys>(); 
+1
source

Try putting all the bad codes in a Hashset and find values ​​that are not in the "hash blacklist"

Here is a good example: fooobar.com/questions/21395 / ...

-one
source

Source: https://habr.com/ru/post/1013789/


All Articles