2012-06-28 9 views
7

Ci sono due liste:Escludere voci di elenco che contengono i valori da un altro elenco

List<string> excluded = new List<string>() { ".pdf", ".jpg" }; 
List<string> dataset = new List<string>() {"valid string", "invalid string.pdf", "invalid string2.jpg","valid string 2.xml" }; 

Come posso filtrare-out valori dalla lista "set di dati", che contiene una qualsiasi parola chiave dalla lista "esclusi"?

+1

Come dice abatishchev, rendere 'excluded' a' HashSet ', specialmente se è grande. – Jodrell

+0

Grazie. Se siamo su HashSet, fornirò questo collegamento a una descrizione di questo argomento: http://stackoverflow.com/questions/1247442/when-should-i-use-the-hashsett-type – lekso

risposta

14
var results = dataset.Where(i => !excluded.Any(e => i.Contains(e))); 
6

Prova:

var result = from s in dataset 
      from e in excluded 
      where !s.Contains(e) 
      select e; 
0
var result=dataset.Where(x=>!excluded.Exists(y=>x.Contains(y))); 

Questo funziona anche quando la lista esclusi è vuoto.

Problemi correlati