Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Except method in list in c#

I had create a two int type list and assign the items that are only in list1 to list1 using except method. for eg

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. 

Please give me suggestion. What is the right way to do it.

like image 727
Lakhae Avatar asked Oct 24 '12 18:10

Lakhae


People also ask

How do you use list except?

Get the difference between two arrays using the Except() method. The following are the two arrays. int[] arr = { 9, 12, 15, 20, 35, 40, 55, 67, 88, 92 }; int[] arr2 = { 20, 35 }; To get the difference, use Except() method that returns the first list, except the elements in the second list.

What is used except method?

The except() method is used to return all items in the given collection except the specified keys. Parameters: The collect() method takes one argument that is converted into the collection and then except() method is applied on it.

What is except in Linq?

The Except() method requires two collections. It returns a new collection with elements from the first collection which do not exist in the second collection (parameter collection). Example: Except in method syntax C#

What is any () in C#?

The Any method checks whether any of the element in a sequence satisfy a specific condition or not. If any element satisfy the condition, true is returned.

What is the use of except method in LINQ?

This LINQ Except () method used to return the elements which are present only in the first list items but not in the second list items. In this syntax, we using except method to comparing two lists to retrieve elements from a1 which not present in a2 list. How except works in LINQ?

What is the difference between except () and except extension method?

The Except () method requires two collections. It returns a new collection with elements from the first collection which do not exist in the second collection (parameter collection). Except extension method doesn't return the correct result for the collection of complex types.

How to get the correct result from except method in Java?

It returns a new collection with elements from the first collection which do not exist in the second collection (parameter collection). Except extension method doesn't return the correct result for the collection of complex types. You need to implement IEqualityComparer interface in order to get the correct result from Except method.

How to implement except method in IEnumerable?

IEnumerable<ProductA> except = fruits1.Except (fruits2); foreach (var product in except) Console.WriteLine (product.Name + " " + product.Code); /* This code produces the following output: orange 4 lemon 12 */ This method is implemented by using deferred execution.


1 Answers

The Except method returns IEnumerable, you need to convert the result to list:

list1 = list1.Except(list2).ToList(); 

Here's a complete example:

List<String> origItems = new List<String>();     origItems.Add("abc");     origItems.Add("def");     origItems.Add("ghi");          List<String> newItems = new List<String>();     newItems.Add("abc");     newItems.Add("def");     newItems.Add("super");     newItems.Add("extra");          List<String> itemsOnlyInNew = newItems.Except(origItems).ToList();     foreach (String s in itemsOnlyInNew){         Console.WriteLine(s);     } 

Since the only items which don't exist in the origItems are super & extra, The result output is :

super

extra

like image 125
Michal Klouda Avatar answered Sep 22 '22 01:09

Michal Klouda