LINQ in Detail
https://www.dotnettricks.com/learn/linq/sql-joins-with-csharp-linq
Single() vs First()
_____________________________________________________________________________
Offtype: Use OfType operator to filter the above collection based on each element's type
Single() vs First()
Single will throw an exception if it finds more than one record matching the criteria. First will always select the first record from the list. If the query returns just 1 record, you can go with
First()
.
Both will throw an
InvalidOperationException
exception if the collection is empty. Alternatively you can use SingleOrDefault()
. This won't throw an exception if the list is empty_____________________________________________________________________________
Offtype: Use OfType operator to filter the above collection based on each element's type
ex:
IList mixedList = new ArrayList();
mixedList.Add(0);
mixedList.Add("One");
mixedList.Add("Two");
mixedList.Add(3);
mixedList.Add(new Student() { StudentID = 1, StudentName = "Bill" });
var stringResult = from s in mixedList.OfType<string>()
select s;
________________________________________________________________
Orderby: A sorting operator arranges the elements of the collection in ascending or descending order. OrderBy sorts the values of a collection in ascending or descending order. It sorts the collection in ascending order by default because
ascending
keyword is optional here. Use descending keyword to sort collection in descending order.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 }
};
var orderByResult = from s in studentList
orderby s.StudentName
select s;
var orderByDescendingResult = from s in studentList
orderby s.StudentName descending
select s;
_______________________________________________________________
THENBY and THEN
Multiple sorting in method syntax is supported by using ThenBy and ThenByDescending extension methods.
THENBY and THEN
Multiple sorting in method syntax is supported by using ThenBy and ThenByDescending extension methods.
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 },
new Student() { StudentID = 6, StudentName = "Ram" , Age = 18 }
};
var thenByResult = studentList.OrderBy(s => s.StudentName).ThenBy(s => s.Age);
var thenByDescResult = studentList.OrderBy(s => s.StudentName).ThenByDescending(s => s.Age);
thenByDescResult would contain following elements. Ram with age 20 comes before Ram with age 18 because it has used ThenByDescending.
____________________________________________________________
GROUPBY
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
new Student() { StudentID = 2, StudentName = "Steve", Age = 21 } ,
new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,
new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
new Student() { StudentID = 5, StudentName = "Abram" , Age = 21 }
};
var groupedResult = from s in studentList
group s by s.Age;
foreach (var ageGroup in groupedResult)
{
Console.WriteLine("Age Group: {0}", ageGroup .Key); //Each group has a key
foreach(Student s in ageGroup) // Each group has inner collection
Console.WriteLine("Student Name: {0}", s.StudentName);
}
var groupedResult = studentList.GroupBy(s => s.Age);
AgeGroup: 18
StudentName: John
StudentName: Bill
AgeGroup: 21
StudentName: Steve
StudentName: Abram
AgeGroup: 20
StudentName: Ram
__________________________________________________________ TOLOOKUP
ToLookup is the same as GroupBy; the only difference is GroupBy execution is deferred, whereas ToLookup execution is immediate. Also, ToLookup is only applicable in Method syntax. ToLookup is not supported in the query syntax
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
new Student() { StudentID = 2, StudentName = "Steve", Age = 21 } ,
new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,
new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
new Student() { StudentID = 5, StudentName = "Abram" , Age = 21 }
};
var lookupResult = studentList.ToLookup(s => s.age);
foreach (var group in lookupResult)
{
Console.WriteLine("Age Group: {0}", group.Key); //Each group has a key
foreach(Student s in group) //Each group has a inner collection
Console.WriteLine("Student Name: {0}", s.StudentName);
}
______________________________________________________________________________
JOINlet's understand join metohod using following Student and Standard class where Student class includes StandardID that matches with StandardID of Standard class.
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = 1, StudentName = "John", StandardID =1 },
new Student() { StudentID = 2, StudentName = "Moin", StandardID =1 },
new Student() { StudentID = 3, StudentName = "Bill", StandardID =2 },
new Student() { StudentID = 4, StudentName = "Ram" , StandardID =2 },
new Student() { StudentID = 5, StudentName = "Ron" }
};
IList<Standard> standardList = new List<Standard>() {
new Standard(){ StandardID = 1, StandardName="Standard 1"},
new Standard(){ StandardID = 2, StandardName="Standard 2"},
new Standard(){ StandardID = 3, StandardName="Standard 3"}
};
var innerJoin = studentList.Join(// outer sequence
standardList, // inner sequence
student => student.StandardID, // outerKeySelector
standard => standard.StandardID, // innerKeySelector
(student, standard) => new // result selector
{
StudentName = student.StudentName,
StandardName = standard.StandardName
});
Join operator in query syntax C#
var innerJoin = from s in studentList // outer sequence
join st in standardList //inner sequence
on s.StandardID equals st.StandardID // key selector
select new { // result selector
StudentName = s.StudentName,
StandardName = st.StandardName
};
Join multiple tables method syntax
var categorizedProducts = product
.Join(productcategory, p => p.Id, pc => pc.ProdId, (p, pc) => new { p, pc })
.Join(category, ppc => ppc.pc.CatId, c => c.Id, (ppc, c) => new { ppc, c })
.Select(m => new {
ProdId = m.ppc.p.Id, // or m.ppc.pc.ProdId
CatId = m.c.CatId
// other assignments
});
______________________________________________________________________________
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.
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);
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 age or not.
bool isAnyStudentTeenAger = studentList.Any(s => s.age > 12 && s.age < 20);
ALL vs ANY
All() is used to detemine if all elements satisfy a condition. Any() tells you whether there are any elements in the sequence at all:
var numbers = new[]{2,4,6,8,9}; var all = numbers.All(n => n % 2 == 0); // returns false - not all numbers are even var any = numbers.Any(); // returns true - there sequence contains at least one element__________________________________________________________
AGGREGATE:
The following example demonstrates Aggregate method that returns comma seperated elements of the string list.
IList<String> strList = new List<String>() { "One", "Two", "Three", "Four", "Five"};
var commaSeperatedString = strList.Aggregate((s1, s2) => s1 + ", " + s2);
___________________________________________________________
AVERAGE
Average extension method calculates the average of the numeric items in the collection. Average method returns nullable or non-nullable decimal, double or float value
IList<Student> studentList = new List<Student>>() {
new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,
new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,
new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 }
};
var avgAge = studentList.Average(s => s.Age);
Console.WriteLine("Average Age of Student: {0}", avgAge);
__________________________________________________________________________
COUNT
The Count operator returns the number of elements in the collection or number of elements that have satisfied the given condition.
IList<Student> studentList = new List<Student>>() {
new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,
new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,
new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
new Student() { StudentID = 5, StudentName = "Mathew" , Age = 15 }
};
var numOfStudents = studentList.Count();
Console.WriteLine("Number of Students: {0}", numOfStudents);
__________________________________________________________________________
SINGLE OR SINGLEORDEFAULT
Single() returns the only element from a collection, or the only element that satisfies the specified condition. If a given collection includes no elements or more than one elements then Single() throws InvalidOperationException.
The SingleOrDefault() method does the same thing as Single() method. The only difference is that it returns default value of the data type of a collection if a collection is empty, includes more than one element or finds no element or more than one element for the specified condition.
IList<int> oneElementList = new List<int>() { 7 };
IList<int> intList = new List<int>() { 7, 10, 21, 30, 45, 50, 87 };
IList<string> strList = new List<string>() { null, "Two", "Three", "Four", "Five" };
IList<string> emptyList = new List<string>();
//following throws error because list contains more than one element which is less than 100
Console.WriteLine("Element less than 100 in intList: {0}", intList.Single(i => i < 100));
//following throws error because list contains more than one element which is less than 100
Console.WriteLine("Element less than 100 in intList: {0}",
intList.SingleOrDefault(i => i < 100));
//following throws error because list contains more than one elements
Console.WriteLine("The only Element in intList: {0}", intList.Single());
//following throws error because list contains more than one elements
Console.WriteLine("The only Element in intList: {0}", intList.SingleOrDefault());
//following throws error because list does not contains any element
Console.WriteLine("The only Element in emptyList: {0}", emptyList.Single());
__________________________________________________________________________
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.
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);
IList<string> strList1 = new List<string>(){"One", "Two", "Three", "Four", "Three"};
IList<string> strList2 = new List<string>(){ "Two", "One", "Three", "Four", "Three"};
bool isEqual = strList1.SequenceEqual(strList2); // returns false
Console.WriteLine(isEqual);
__________________________________________________________________________
TakeWhile
String colection = { "one", "two", "three", "four", "five" };
var strings = collection.TakeWhile(n => n.Length < 4);
Skip
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var firstNumbersLessThan6 = numbers.Skip(3);
output of this example will be 3, 9, 8, 6, 7, 2 and 0.
Comments
Post a Comment