Here we have a string array of countries and we need to return the countries from the first collection, those are not present in the second collection. Now run the application and see the output. In spite of having the country UK in the second collection, it still shows in the output. This is because the default comparer that is being used to filter the data is case-insensitive. So if you want to ignore the case-sensitive then you need to use the other overloaded version of the Except method which takes IEqualityComparer as an argument.
So, modify the program as shown where we pass StringComparer as an argument and this StringComparer class already implements the IEqualityComparer interface.
Now run the application and it will display the data as expected. Let us understand this with an example. Create a class file with the name Student. This is a very simple student class with just two properties. Let say, we have the following two data sources.
As you can see in the above image, we have two data sources. The first data source i. AllStudents hold the information of all the students while the second data source i. For example, suppose you have two lists and you are told to determine the common elements from the two lists.
How will you do it without LINQ? You will use a loop to find the common elements of both lists. But you can do this task in a single line of code using of LINQ functions.
LINQ provides a convenient syntax and many useful methods for operating with collections of objects. All these methods are in the System. Linq namespace. The following explains the details of these functions with code examples. This is the LINQ extension method that subtracts elements from a collection.
For example this will remove elements of the first entities that are elements in the second entities and that will be returned as a third entity. The Except extension method provides a fast way to use set logic. It removes all the elements from one array found in another array. This reduces the need for complex foreach loops. This browser is no longer supported.
Download Microsoft Edge More info. Contents Exit focus mode. Except Method Definition Namespace: System. Linq Assembly: System. Produces the set difference of two sequences. TSource The type of the elements of the input sequences.
Applies to. Is this page helpful? Yes No.
0コメント