Hi = stu.FirstName, Gender = stu.Gender }); foreach (var

 Hi AllThis blog will be covering some of the most important LINQ functions & queries that are being used in our day-to-day development of applications.                               Aggregate FunctionAn aggregate function is a function where the values of multiple collection are grouped together as input on certain criteria to form a single value of greater significance or measurement such as a set.Common Aggregate Functions are:1.SUM() :- This function Returns the sum of column values.Example:- EmployeeDataContext employee = new EmployeeDataContext();/*Total Salaries for all employee */ var empSalarySum = (from emp in employee.EMPLOYEES                         select emp.SALARY).Sum();2.AVERAGE() :-This function Returns the average of column values.Example:-EmployeeDataContext employee = new EmployeeDataContext ();/*Averge of whole salary in Employee table*/   var empSalaryAvg = (from emp in employee.EMPLOYEES                         select emp.SALARY).Average(); 3.COUNT() :-This function Returns the total number of rows in a table.Example:-EmployeeDataContext employee = new EmployeeDataContext ();var empCount = (from emp in employee.EMPLOYEES                         select emp.Id).Count();4.MAX() :-This function Returns the maximum value in the column.Example:-EmployeeDataContext employee = new EmployeeDataContext ();var empSalaryMax = (from emp in employee.EMPLOYEES                         select emp.SALARY).Max();5.MIN() :-This function Returns the minimum value in the column.Example:-EmployeeDataContext employee = new EmployeeDataContext ();var empSalaryMin = (from emp in employee.EMPLOYEES                         select emp.SALARY).Min();       Projection Operators There are 2 important operators that belong to Projection Operator that is as follows:-1)Select2)SelectManyThese Projection Operators (Select & SelectMany) are used to transform the results into a query.Select clause :- In SQL Select clause allows us to specify what or which  columns we want to retrieve. like this   Lanquage integrated query(LINQ) using SELECT  operator we can specify the properties that we want to retrieve.. It also allows us to perform calculations on Data.Example-1:-Let’s  take a example of student information and fetch information by Select operator and see how to work Select operator.var result = Student.GetAllStudents().Select(stu => new  {                                FirstName = stu.FirstName,                               Gender = stu.Gender    }); foreach (var v in result) {   Console.WriteLine(v.FirstName + ” – ” + v.Gender); } OUTPUTAmit      : MaleDeepak : MaleNeeraj  : MaleSeema  : FeMale Example-2:-In This example we are see how to work  SelectMany operator.Here the list of students and all subject strings of a given a student to an IEnumerable. In this we have four students, there will be four IEnumerable sequences, which are then flattened to form a single sequence i.e a single IEnumerable sequence.IEnumerable allSubjects = Student.GetAllStudetns().SelectMany(s => s.Subjects);foreach (string subject in allSubjects){    Console.WriteLine(subject);}   Ordering Operators in LINQIn this topic we will learn What is Ordering Operators ,Type of Ordering Operator and How to work Ordering Operators.The following 5 important LINQ query operators that belong to Ordering Operators category:-1.OrderBy 2.OrderByDescending 3.ThenBy 4.ThenByDescending 5.Reverse Now lets understand one by one all above mentioned Ordering operators with example.OrderBy:-OrderBy  operator are used to sort Data in ascending or descending order.Example:- Lets take a example of students and We will use the following Student class in this Example. public class Student{    public int StuID { get; set; }    public string StudentName { get; set; }    public int TotalMarksofStudents { get; set; }    public static List GetAllStudentsInfo()    {        List listOfStudents = new List        {            new Student            {                StuID= 101,                StudentName = “Tom”,                TotalMarksofStudents = 800            },            new Student            {                StuID= 102,                StudentName = “Mary”,                TotalMarksofStudents = 900            },            new Student            {                StuID= 103,                StudentName = “Valarie”,                TotalMarksofStudents = 800            },            new Student            {                StuID= 104,                StudentName = “John”,                TotalMarksofStudents = 800            },        };        return listofStudents;    }} Sort Students by Name in ascending order:-IEnumerable resultofStudent = Student.GetAllStudentsInfoo().OrderBy(s => s.StudentName);foreach (Student student in resultofStudent){Console.WriteLine(student.StudentName);}OutputSource:http://4.bp.blogspot.comLets Sort Students by Name in descending orderIEnumerable resultofStudent = Student.GetAllStudentsinfo().OrderByDescending(s => s.StudentName);foreach (Student student in resultofStudent){Console.WriteLine(student.StudentName);}OutputSource:http://4.bp.blogspot.comThenBy:-ThenBy  operator are used to sort Data.Example:- Lets take a example of students and We will use the above Student class in this Example.OrderBy and OrderByDescending it is work fine when we want to sort a Data just by one value or expression.If we want to sort by more than one Data or collection, that’s when we use ThenBy or ThenByDescending along with OrderBy or OrderByDescending.OrderBy or OrderByDescending is widely  used for the primary sort and ThenBy or ThenByDescending is used for performing secondary sort in LINQ query. We can use Secondary Sort operators (ThenBy or ThenByDescending )  more than once in the same LINQ query.Example :-  a) Sorts Students first by TotalMarksofStudent in ascending order(using Primary Sort) b) The four Students with TotalMarksofStudent of 800,  then we will  sort students by its Name in ascending order (using First Secondary Sort) c) The Two Students with Name of John, then we will  sort students  by StuID in ascending order (using Second Secondary Sort) IEnumerable resultofStudent = Student.GetAllStudetnsinfo().OrderBy(s => s.TotalMarksofStudents).ThenBy(s => s.StudentName).ThenBy(s => s.StuID);foreach (Student student in resultofStudent){Console.WriteLine(student.TotalMarksofStudent + ” ” + student.StudentName + ” ” + student.StuID);} OutputSource:http://4.bp.blogspot.comReverse:-Reverse operator are used to simply reverses the items in a given collection.Example:- Lets take a example of students and We will use the above Student class in this Example. IEnumerable studentResult = Student.GetAllStudetnsinfo();Console.WriteLine(“Before calling Reverse”);foreach (Student s in studentResult){Console.WriteLine(s.StuID + ” ” + s.StudentName + ” ” + s.TotalMarksofStudent);}Console.WriteLine();IEnumerable studentResult = studentResult.Reverse();Console.WriteLine(“After calling Reverse”);foreach (Student s in studentResult){Console.WriteLine(s.StuID + ” ” + s.StudentName + ” ” + s.TotalMarksofStudent);} OutputsSource:http://4.bp.blogspot.comPartitioning Operators in LINQIn this topic we will learn What is Partitioning Operators in LINQ ,Partitioning Operators and How to work Partitioning Operators.The following four important LINQ query operators That is belong to Partitioning Operators:- 1.Take 2.Skip 3.TakeWhile 4.SkipWhileNow lets understand one by one all above mentioned Partitioning Operators with example.Take:-Take method is used to returns a specified number of elements given by he/she from the starting of the collection. The number of items  to return is specified using the count parameter this method expects.Lets understand it by a example.Example:-In this example we have a array of countries and we will take only the first 3 countries of the array by using Take operator. string  listCountries = { “Australia”, “Canada”, “Germany”, “US”, “India”, “UK”, “Italy” };IEnumerable conResult = listCountries.Take(3);foreach (string country in conResult) { Console.WriteLine(country); } OutputSource:http://4.bp.blogspot.comSkip:-This method skips a specified number of elements in a collection and after that returns the remaining elements. The number of items that will skip is specified by the count parameter which is given by us to method expects.Example:-In this example we have a array of countries and we will skips only the first 3 countries of the array by using skip operator. string listCountries = { “Australia”, “Canada”, “Germany”, “US”, “India”, “UK”, “Italy” };IEnumerable countryResult = listCountries.Skip(3);foreach (string country in countryResult) { Console.WriteLine(country); } OutputSource:http://4.bp.blogspot.comTakeWhile:-This method returns a specified number of elements in a collection as long as the specified condition will true.Example:-In this example we have a array of countries and we will get specific number of elements by using TakeWhile operator. string listCountries = { “Australia”, “Canada”, “Germany”, “US”, “India”, “UK”, “Italy” };IEnumerable countryResult = listCountries.TakeWhile(s => s.Length > 2);foreach (string country in countryResult) { Console.WriteLine(country); } OutputSource:http://4.bp.blogspot.com  Group-By OperatorGroupBy operator is work like group by operator in SQL that means it is use for get group of collection according to user condition like groupby name,id etc.In other words GroupBy operator  creates and returns a sequence of IGrouping.Example:-Lets take a example of EmployeeInfo class for understand how to GroupBy operator work. public class EmployeeInfo {     public int EmpID { get; set; }     public string EmpEmpName { get; set; }     public string EmpGender { get; set; }     public string EmpEmpDepartment { get; set; }     public int EmpEmpSalary { get; set; }    public static List GetAllEmployeesInfo()     {         return new List()         {             new EmployeeInfo  { EmpID = 1, EmpName = “Mark”, EmpGender = “Male”,                                          EmpDepartment = “IT”, EmpSalary = 45000 },             new EmployeeInfo  { EmpID = 2, EmpName = “Steve”, EmpGender = “Male”,                                          EmpDepartment = “HR”, EmpSalary = 55000 },             new EmployeeInfo  { EmpID = 3, EmpName = “Ben”, EmpGender = “Male”,                                          EmpDepartment = “IT”, EmpSalary = 65000 },             new EmployeeInfo  { EmpID = 4, EmpName = “Philip”, EmpGender = “Male”,                                          EmpDepartment = “IT”, EmpSalary = 55000 },             new EmployeeInfo  { EmpID = 5, EmpName = “Mary”, EmpGender = “Female”,                                          EmpDepartment = “HR”, EmpSalary = 48000 },             new EmployeeInfo  { EmpID = 6, EmpName = “Valarie”, EmpGender = “Female”,                                          EmpDepartment = “HR”, EmpSalary = 70000 },             new EmployeeInfo  { EmpID = 7, EmpName = “John”, EmpGender = “Male”,                                          EmpDepartment = “IT”, EmpSalary = 64000 },             new EmployeeInfo  { EmpID = 8, EmpName = “Pam”, EmpGender = “Female”,                                          EmpDepartment = “IT”, EmpSalary = 54000 },             new EmployeeInfo  { EmpID = 9, EmpName = “Stacey”, EmpGender = “Female”,                                          EmpDepartment = “HR”, EmpSalary = 84000 },             new EmployeeInfo  { EmpID = 10, EmpName = “Andy”, EmpGender = “Male”,                                          EmpDepartment = “IT”, EmpSalary = 36000 }         };     } }1)Get Employee Count By EmpDepartment wize.var empGroup = from employee in EmployeeInfo.GetAllEmployeesInfo() group employee by employee.EmpDepartment;foreach (var group in empGroup) { Console.WriteLine(“{0} – {1}”, group.Key, group.Count()); }OutputSource:http://1.bp.blogspot.com 2)Get all Employee Count By EmpDepartment and we can  also get each employee and department name.var empGroup = from employee in Employee.GetAllEmployeesInfo() group employee by employee.EmpDepartment;foreach (var group in empGroup) { Console.WriteLine(“{0} – {1}”, group.Key, group.Count()); Console.WriteLine(“———-“); foreach (var employee in group) { Console.WriteLine(employee.EmpName + ” ” + employee.EmpDepartment); } Console.WriteLine(); Console.WriteLine(); }OutputSource:http://2.bp.blogspot.com JOINSJoins is the most important part of Language integrated query(LINQ) so that lets discuss about joins and see how it is work with the help of example. for now lets first look into its definition that is as follow:-Join:-The Join  widely  useful for associating elements from different collections that have no direct relationship in the object class or model. The only purpose is that the elements in each collection share some value that can be compared for equal values.There are several type of joins lets understand one by one. 1.INNER JOIN :- Inner join is use to take only matching elements from two or more collections and non-matching elements are not come in result set.Let’s understand Inner Join with an example. lets take  the following DepartmentInfo and EmployeeInfo classes. focus that, Employee Andy does not assigned to any department and when we do An inner join it will not include his record in the result set. public class DepartmentInfo {     public int DeptID { get; set; }     public string Name { get; set; }    public static List GetAllDepartmentsInfo()     {         return new List()         {             new DepartmentInfo { DeptID = 1, Name = “IT”},             new DepartmentInfo { DeptID = 2, Name = “HR”},             new DepartmentInfo { DeptID = 3, Name = “Payroll”},         };     } }public class EmployeeInfo {     public int EmpID { get; set; }     public string EmpName { get; set; }     public int DepartmentID { get; set; }    public static List GetAllEmployeesInfo()     {         return new List()         {             new EmployeeInfo { EmpID = 1, EmpName = “Mark”, DepartmentID = 1 },             new EmployeeInfo { EmpID = 2, EmpName = “Steve”, DepartmentID = 2 },             new EmployeeInfo { EmpID = 3, EmpName = “Ben”, DepartmentID = 1 },             new EmployeeInfo { EmpID = 4, EmpName = “Philip”, DepartmentID = 1 },             new EmployeeInfo { EmpID = 5, EmpName = “Mary”, DepartmentID = 2 },             new EmployeeInfo { EmpID = 6, EmpName = “Valarie”, DepartmentID = 2 },             new EmployeeInfo { EmpID = 7, EmpName = “John”, DepartmentID = 1 },             new EmployeeInfo { EmpID = 8, EmpName = “Pam”, DepartmentID = 1 },             new EmployeeInfo { EmpID = 9, EmpName = “Stacey”, DepartmentID = 2 },             new EmployeeInfo { EmpID = 10, EmpName = “Andy”}         };     } } Example:- Now Join the EmployeesInfo and DepartmentInfo collections and print all the Employees and their corresponding department names. var resultSet = EmployeeInfo.GetAllEmployeesInfo().Join(DepartmentInfo.GetAllDepartmentsInfo(), e => e.DepartmentID, d => d.DeptID, (employee, department) => new { EmployeeName = employee.EmpName, DepartmentName = department.Name }); foreach (var employee in resultSet) { Console.WriteLine(employee.EmployeeName + ” ” + employee.DepartmentName); } Output:- See that, in the resultSet we don’t have Andy record. This is happaning  because, Andy does not have a matching department in Department collection. So this is effectively an inner join.Source:http://2.bp.blogspot.com 2.Left Outer Jon:- LEFT OUTER JOIN widely use to take  the matching elements plus all the non matching elements from the left collection are included in the result set.Example:- Now do Left Outer Join in  EmployeesInfo and DepartmentInfo collections and print all the Employees and their corresponding department names.public class DepartmentInfo {     public int DeptID { get; set; }     public string Name { get; set; }    public static List GetAllDepartmentsInfo()     {         return new List()         {             new DepartmentInfo { DeptID = 1, Name = “IT”},             new DepartmentInfo { DeptID = 2, Name = “HR”},             new DepartmentInfo { DeptID = 3, Name = “Payroll”},         };     } }public class EmployeeInfo {     public int EmpID { get; set; }     public string EmpName { get; set; }     public int DepartmentID { get; set; }    public static List GetAllEmployeesInfo()     {         return new List()         {             new EmployeeInfo { EmpID = 1, EmpName = “Mark”, DepartmentID = 1 },             new EmployeeInfo { EmpID = 2, EmpName = “Steve”, DepartmentID = 2 },             new EmployeeInfo { EmpID = 3, EmpName = “Ben”, DepartmentID = 1 },             new EmployeeInfo { EmpID = 4, EmpName = “Philip”, DepartmentID = 1 },             new EmployeeInfo { EmpID = 5, EmpName = “Mary”, DepartmentID = 2 },             new EmployeeInfo { EmpID = 6, EmpName = “Valarie”, DepartmentID = 2 },             new EmployeeInfo { EmpID = 7, EmpName = “John”, DepartmentID = 1 },             new EmployeeInfo { EmpID = 8, EmpName = “Pam”, DepartmentID = 1 },             new EmployeeInfo { EmpID = 9, EmpName = “Stacey”, DepartmentID = 2 },             new EmployeeInfo { EmpID = 10, EmpName = “Andy”}         };     } } var resultSet = from e in EmployeeInfo.GetAllEmployeesInfo() join d in DepartmentInfo.GetAllDepartmentsInfo() on e.DepartmentID equals d.DeptID into eGroup from d in eGroup.DefaultIfEmpty() select new { EmployeeName = e.EmpName, DepartmentName = d == null ? “No Department” : d.Name };foreach (var v in resultSet) { Console.WriteLine(v.EmployeeName + ” ” + v.DepartmentName); } OutputSource:http://2.bp.blogspot.com As we all know every lanquage is wide but i try to  cover few of important topics of LINQ in this blog hope you all are like it. Anythough for further queries, feel free to place your valuable comments in the comments section below!