In my previous article we learned what LINQ is.
If you missed it by chance then click here.
There are the following two ways to write LINQ queries using the Standard Query operators, in other words Select, From, Where, Orderby, Join, Groupby and many more.
- Using lambda expressions.
- Using SQL like query expressions.
The standard query operators are implemented as extension methods on an IEnumerable<T> interface. So, all the generic and non-generic classes implement IEnumerable<T>. That means on all these types we can use the LINQ query operators.
Demo
In my console application, I have a class Employee with three auto-implemented properties Id, Name and gender.
The Employee class also has a GetEmployees method that returns a List<Employee>. The list class is in the System.Collection.Generic namespace and we know all the classes present in this namespace implements IEnumerable<T>. That means we can use the LINQ query operators in the List.
- using System.Collections.Generic;
-
- namespace WritingLinqQueries {
- class Employees {
-
- public int Id { get; set; }
- public string Name { get; set; }
- public string Gender { get; set; }
-
-
- public static List<Employees> GetEmployees() {
-
- List<Employees> employeeList = new List<Employees>();
-
-
- Employees employeeOne = new Employees {
- Id = 1, Name = "Sara", Gender = "Female"
- };
-
- employeeList.Add(employeeOne);
-
- Employees employeeTwo = new Employees {
- Id = 2, Name = "James", Gender = "Male"
- };
- employeeList.Add(employeeTwo);
-
- Employees employeeThree = new Employees {
- Id = 3, Name = "Lara Croft", Gender = "Female"
- };
- employeeList.Add(employeeThree);
-
- Employees employeeFour = new Employees {
- Id = 4, Name = "Max", Gender = "Male"
- };
- employeeList.Add(employeeFour);
-
- Employees employeeFive = new Employees {
- Id = 5, Name = "Aiden", Gender = "Male"
- };
- employeeList.Add(employeeFive);
-
-
- return employeeList;
- }
- }
- }
Using Lambda Expression
In the main program we need to call the employees GetEmployees method and for that we can write the following in our main method:
- using System;
- using System.Collections.Generic;
- using System.Linq;
-
- namespace WritingLinqQueries {
- class Program {
- static void Main() {
-
- IEnumerable<Employees> employee = Employees.GetEmployees().Where(emp => emp.Gender == "Female");
-
-
- foreach (Employees e in employee) {
- Console.WriteLine(e.Name);
- }
- }
- }
- }
The other way to do it is by using the SQL like query.
- using System;
- using System.Collections.Generic;
- using System.Linq;
-
- namespace WritingLinqQueries {
- class Program {
- static void Main() {
-
- IEnumerable<Employees> employee = from emp in Employees.GetEmployees() where emp.Gender == "Male" select emp;
-
- foreach (Employees e in employee) {
- Console.WriteLine(e.Name);
- }
- }
- }
- }
Note
From a performance perspective there is no difference between the two because behind the scene, LINQ queries written using SQL like query expressions are translated into their lambda expressions before they are compiled.
The next article of this series explains extension methods.
Until then keep learning.
Thank you,