- Local Sequence implements IEnumerable <T> interface.
- Remote Sequence implements IQueryable<T> interface.
An example of Local Sequence
- class Program
- {
- static void Main(string[] args)
- {
- int[] list =
- {
- 1,
- 2,
- 3,
- 5,
- 6,
- 7,
- 8
- };
- foreach(var item in list)
- Console.WriteLine(item);
- Console.ReadKey();
- }
- }
Output
It would simply display the numbers in the list. Now we will see how we can manipulate this data with the LINQ.
Example: Display those numbers which are greater than two.
Code
- class Program
- {
- static void Main(string[] args)
- {
- int[] list =
- {
- 1,
- 2,
- 4,
- 5,
- 6,
- 7,
- 8
- };
- var result = list.Where(x => x > 2);
- foreach(var item in result)
- Console.WriteLine(item);
- Console.ReadKey();
- }
- }
Output
You can see that how we can easily manipulate data and can get resuls according to our requirements.
2. Deferred Execution
It means that the execution of LINQ query does not execute at the time of creation. Rather it is executed when it is used. Actually the execution of the LINQ query starts with the foreach loop.
Example:
Code:
- class Program
- {
- static void Main(string[] args)
- {
- int[] list =
- {
- 1,
- 2,
- 3,
- 5,
- 6,
- 7,
- 8
- };
- var result = list.Where(x => x > 2);
- list[0] = 99;
-
- foreach(var item in result) {
- Console.WriteLine(item);
- }
- Console.ReadKey();
- }
- }
Output
Notice that result 99 has been included even at that time the query was constructed. There are number of operators that run at the time of query creation such as ToArray, ToDictionery, etc.
Example:
Code:
- class Program
- {
- static void Main(string[] args)
- {
- int[] list =
- {
- 1,
- 2,
- 4,
- 5,
- 6,
- 7,
- 8
- };
- var result = list.Where(x => x > 2).ToArray();
- list[0] = 99;
- foreach(var item in result) {
- Console.WriteLine(item);
- }
- Console.ReadKey();
- }
- }
Output
Notice that 99 is not included because of additional ToArray().
Styles of writing LINQ Query
There are two styles of writing LINQ queries.
- Fluent Style
- Query Expression Style
Query Expression Style is mostly used because its syntax makes more sense.
An example of Fluent Style
Code
- class Employee
- {
- public string Name
- {
- set;
- get;
- }
- public int Salary
- {
- set;
- get;
- }
- static void Main(string[] args)
- {
- Employee[] employee =
- {
- new Employee
- {
- Name = "Usman", Salary = 50000
- },
- new Employee
- {
- Name = "Ayaz", Salary = 60000
- },
- new Employee
- {
- Name = "Ahmad", Salary = 70000
- }
- };
- var emploee_List = employee.Where(x => x.Salary > 50000)
- .OrderBy(x => x.Name)
- .Select(x => x.Name);
-
- foreach(var emp in emploee_List)
- {
- Console.WriteLine(emp);
- }
- Console.ReadKey();
- }
- }
Output
An example of Query Expression Style
Code:
- class Employee
-
- {
-
- public string Name
- {
- set;
- get;
- }
-
- public int Salary
- {
- set;
-
- get;
- }
-
- static void Main(string[] args)
-
- {
-
- Employee[] employee =
- {
-
- new Employee
- {
- Name = "Usman", Salary = 50000
- },
-
- new Employee
- {
- Name = "Ayaz", Salary = 60000
- },
-
- new Employee
- {
- Name = "Ahmad", Salary = 70000
- }
-
- };
-
- var emploee_List =
-
- from i in employee where i.Salary > 50000
-
- orderby i.Name
-
- select i.Name;
-
- foreach(var emp in emploee_List)
-
- {
-
- Console.WriteLine(emp);
-
- }
-
- Console.ReadKey();
-
- }
-
- }
Output
You can see that Expression Query Styles makes more sense than Fluent Query Style. In the next article, I will write about operators and joins in LINQ.