2012-10-24 17 views
21

Mam utworzyć listę typu dwóch int i przypisać elementy, które są tylko na liście 1 do listy 1 przy użyciu metody except. na przykład:Jak używać Z wyjątkiem metody na liście C#

List<int> list1 = new List<int>(); 
List<int> list2 = new List<int>(); 

list1 = {1,2,3,4,5,6} // get items from the database 
list2 = {3,5,6,7,8} // get items from the database 

list1 = list1.Except(list2); // gives me an error. 

Proszę dać mi sugestię. Jak to zrobić?

Odpowiedz

46

Sposób Except zwraca IEnumerable, trzeba przekonwertować wynik do listy:

list1 = list1.Except(list2).ToList(); 
Powiązane problemy