Wednesday, September 27, 2017

Different ways of writing Linq Queries




There exists 3 ways to write linq queries:

1.   Query Expression (Query syntax) (Sql like query syntax)
Its like sql query syntax. The result of query syntax expression is a query object, which is usually a collection of type IEnumerable<T> or IQueryable<T>. At compile time, the query expression is converted into method invocation.

Query expression syntax is

from[identifier]
in [source collection]
let [expression]
where [Boolean expression]
order by [expression (ascending/descending)]
Select [exression]
group [expression]     by [expression]
into   [expression]

for eg.
List<int> numbers = new List<int>() {1,2,3,4,5,6,7,8,9,10};
IEnumerable<int> result =  from num in numbers
                                         Where num > 5 && num < 10
                                         Select num;    // result = 6,7,8,9

Note: Behind the scene, linq queries written using Query Expression syntax like above, are translated into lambda expressions before they are compiled.

To write LINQ queries we use the LINQ Standard Query Operators. The following are a few Examples of Standard Query Operators
select, from, where , orderby , join, groupby.

The standard query operators are implemented as extension methods on IEnumerable<T> interface.


LINQ Standard Query Operators also called as LINQ extension methods can be broadly classified into the following categories
Aggregate Operators
Grouping Operators
Restriction Operators
Projection Operators
Set Operators
Partitioning Operators
Conversion Operators
Element Operators
Ordering Operators
Generation Operators
Query Execution
Join Operators
Custom Sequence Operators

2.     Method Invocation (Extension Method Syntax)
It uses Lambda expression to write linq queries. The result of method syntax is also a query object same as Query Expression/Query syntax.
Its syntax is :
[source collection]
     . Where [Boolean expression]
     . order by [expression (ascending/descending)]
     . Select [exression]
 . GroupBy [expression]

For eg. 
IEnumerable<int> result = numbers.Where(n=> n>5 && n < 10);
LINQ query using Lambda Expressions.
IEnumerable<Student> students = Student.GetAllStudents()
                                                             .Where(student => student.Gender == "Male");

LINQ query using using SQL like query expressions
IEnumerable<Student> students = from student in Student.GetAllStudents()
                                                         where student.Gender == "Male"
                                               select student;


3.   Mixed Syntax
We can use a mixture of both Query expression syntax and Method Invocation syntax by enclosing the query expression inside parenthesis and make a call to method. eg.
int res = (from num in numbers
                where num > 5 && num <10
                Select num).Count();    // result = 4
 

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More