The Except operator produces the set difference between two sequences.
public static IEnumerable<TSource> Except<TSource>(
this IEnumerable<TSource> first, IEnumerable<TSource> second);
public static IEnumerable<TSource> Except<TSource>(
this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer);
Now, lets take a look at example:we will start by creating a new Console Application project, i have a collection of City which have some properties like:
public class City{
public string Name { get; set; }
public string Country { get; set; }
public bool isRead { get; set; }
public string CreatedBy { get; set; }
}
I will initial collection of City by creating chain intial City object
var cities = new List<City>{
new City {Country = 'Vietnam', Name = 'Ha Noi', isRead=false,CreatedBy='quachnguyen'},
new City {Country = 'USA', Name = 'New York', isRead=false,CreatedBy='code2code'},
new City {Country = 'USA', Name = 'Sri lanka', isRead=false,CreatedBy='quachnguyen'},
new City {Country = 'Japan', Name = 'Tokyo', isRead=true,CreatedBy='quachnguyen'},
new City {Country = 'Spain', Name = 'Milan', isRead=false,CreatedBy='code2code'},
new City {Country = 'Spain', Name = 'Real Madrid', isRead=true,CreatedBy='code2code'},
new City {Country = 'Spain', Name = 'Barcelona', isRead=true,CreatedBy='quachnguyen'},
new City {Country = 'Netherland', Name = 'Amsterdam', isRead=false,CreatedBy='quachnguyen'},
new City {Country = 'Vietnam', Name = 'Ho Chi Minh', isRead=false,CreatedBy='quachnguyen'},
};
I have to get a collection of city which the user cannot read (isRead=false) but except the Cities which have created by quachnguyen (CreatedBy=='quachnguyen') so the first of all i will get the list of cities are not created by quachnguyen and don't have the read permission.
var notMyCities = cities.Where(c => c.CreatedBy != 'quachnguyen' && c.isRead == false);
And then i use Except keywork to except notMyCities:
var result = cities.Except(notMyCities);In the end i will loop through the result and display cities in the screen:
Console.WriteLine('-------------------------------------------------------------');
Console.WriteLine(String.Format("{0,-15} | {1,-15} | {2,-15} | {3,-15}", 'Country', " Name ", " Created By ",
"is Read"));
Console.WriteLine('-------------------------------------------------------------');
foreach (var item in result)
{
Console.WriteLine(String.Format("{0,-15} | {1,-15} | {2,-15} | {3,-15}", item.Country, item.Name,
item.CreatedBy, item.isRead));
}
Console.WriteLine('-------------------------------------------------------------');
Console.Read();Press F5 and the result will be show in the console as bellow:
The Except operator allocates and returns an enumerable object that captures the arguments passed to the operator. An ArgumentNullException is thrown if any argument is null.
When the object returned by Except is enumerated, it enumerates the first sequence, collecting all distinct elements of that sequence. It then enumerates the second sequence, removing those elements that were also contained in the first sequence. It finally yields the remaining elements in the order in which they were collected. If a non-null comparer argument is supplied, it is used to compare the elements. Otherwise the default equality comparer, EqualityComparer<TSource>.Default, is used.
Không có nhận xét nào:
Đăng nhận xét