Monday, June 4, 2018

Linq Operators/Methods

Except

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#
            
IList<string> strList1 = new List<string>(){"One", "Two", "Three", "Four", "Five" };
IList<string> strList2 = new List<string>(){"Four", "Five", "Six", "Seven", "Eight"};

var result = strList1.Except(strList2);

foreach(string str in result)
        Console.WriteLine(str);

Output:
One 
Two
Three

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.

SequenceEqual

The SequenceEqual method checks whether the number of elements, value of each element and order of elements in two collections are equal or not.If the collection contains elements of primitive data types then it compares the values and number of elements, whereas collection with complex type elements.
Example: SequenceEqual in Method Syntax C#

IList<string> strList1 = new List<string>(){"One", "Two", "Three", "Four", "Three"};

IList<string> strList2 = new List<string>(){"One", "Two", "Three", "Four", "Three"};

bool isEqual = strList1.SequenceEqual(strList2); // returns true
Console.WriteLine(isEqual);

Output:
trueIf the order of elements are not the same then SequenceEqual() method returns false

Concatenation Operator: Concat

The Concat() method appends two sequences of the same type and returns a new sequence (collection).
Example: Concat in C#

IList<int> collection1 = new List<int>() { 1, 2, 3 };
IList<int> collection2 = new List<int>() { 4, 5, 6 };

var collection3 = collection1.Concat(collection2);

foreach (int i in collection3)
    Console.WriteLine(i);

Output:
1
2
3
4
5
6
OperatorDescription
AllChecks if all the elements in a sequence satisfies the specified condition
AnyChecks if any of the elements in a sequence satisfies the specified condition
ContainChecks if the sequence contains a specific element

All:

The All operator evalutes each elements in the given collection on a specified condition and returns True if all the elements satisfy a condition.
Example: All operator C#

IList<Student> studentList = new List<Student>() { 
        new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
        new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 } ,
        new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
        new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
        new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 } 
    };

// checks whether all the students are teenagers    
bool areAllStudentsTeenAger = studentList.All(s => s.Age > 12 && s.Age < 20);

Console.WriteLine(areAllStudentsTeenAger);

Any:

Any checks whether any element satisfy given condition or not? In the following example, Any operation is used to check whether any student is teen ager or not.
Example: Any operator C#

bool isAnyStudentTeenAger = studentList.Any(s => s.age > 12 && s.age < 20);

Set operator: Intersect

The Intersect extension method requires two collections. It returns a new collection that includes common elements that exists in both the collection. Consider the following example.
Example: Intersect in method syntax C#
            
IList<string> strList1 = new List<string>() { "One", "Two", "Three", "Four", "Five" };
IList<string> strList2 = new List<string>() { "Four", "Five", "Six", "Seven", "Eight"};

var result = strList1.Intersect(strList2);

foreach(string str in result)
        Console.WriteLine(str);

Output:
Four Five
The Intersect 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 Intersect method

Set operator: Union

The Union extension method requires two collections and returns a new collection that includes distinct elements from both the collections. Consider the following example.
Example: Union() in C#
            
IList<string> strList1 = new List<string>() { "One", "Two", "three", "Four" };
IList<string> strList2 = new List<string>() { "Two", "THREE", "Four", "Five" };

var result = strList1.Union(strList2);

foreach(string str in result)
        Console.WriteLine(str);

Output:
One  Two  three  THREE  Four  Five




No comments:

Post a Comment

javascript Filter/index off

 var family = [{"name":"Jack",  "age": 26},               {"name":"Jill",  "age"...