Header add


In this article we will learn
  • Linq Syntax and Query
  • How to write Linq Query
  • Query Syntax and Method Syntax
LINQ Syntax are divided into two parts.
  1. Query Syntax
  2. Method Syntax
Query Syntax

This is one of the easy ways to write complex LINQ queries in an easy and readable format. The syntax for this type of query is very much similar to SQL Query. If you are familiar with SQL queries then it is going to be easy for you to write LINQ queries using this query syntax. It has SQL like query syntax but it does not support all query operators in LINQ. Method Syntax is the more powerful way to write LINQ queries and it is also a standard way to write LINQ syntax.The syntax is given below.
    from <range variable> in <IEnumerable<T> or IQueryable<t> collection>  
     <standard query operators> <lambda expression>  
     <select or groupBy operator> <result formation  
                        OR
    from object in datasource
    where condition
    select object;
Code Explanation
  • from object in datasource are the initialization point.
  • where is use for conditional based.
  • object is used for selection
Let's take console application and create a LINQ example of a employee list and filter it accordingly.
    using System;  
     using System.Collections.Generic;  
     using System.Linq;  
     using System.Text;  
     using System.Threading.Tasks;  
     namespace LinqSeries  
     {  
       public class Program  
       {  
         static void Main(string[] args)  
         {  
           //Data Source  
           List<string> employeeList = new List<string>()  
           {  
             "John Thomas","Lori Orona","Nick Jonas","John Mathew","Wonda Kher","Simon Ardino"  
           };  
           //LINQ Query using Query Syntax  
           var QuerySyntax = from obj in employeeList  
                    where obj.Contains("John")  
                    select obj;  
           //Execution  
           foreach (var Queryitem in QuerySyntax)  
           {  
             Console.Write(Queryitem + " ");  
           }  
           Console.ReadLine();  
         }  
       }  
     }  
Code Explanation
  • We consider a dummy employee list - the employee list define the datasource.
  • where condition is used for filter the record ( you can do it as per your requirement).
  • select object is used for selection the filter list

After run the application the output be print "John Thomas" and "John Mathew" as per our filter.


Method Syntax

Method syntax becomes most popular now a day to write LINQ queries. It uses a lambda expression to define the condition for the query. Method syntax are easy to write simple queries to perform read-write operations on a particular data source. But for complex queries Method syntax are a little hard to write as compared to query syntax.

In this approach, the LINQ query is written by using multiple methods by combining them with a dot (.). The Syntax is given below:
    DataSource.ConditionMetod().SelectionMethod()
Let's consider same example of employee list and see how method syntax will work.

    using System;  
     using System.Collections.Generic;  
     using System.Linq;  
     using System.Text;  
     using System.Threading.Tasks;  
     namespace LinqSeries  
     {  
       public class Program  
       {  
         static void Main(string[] args)  
         {  
           //Data Source  
           List<string> employeeList = new List<string>()  
           {  
             "John Thomas","Lori Orona","Nick Jonas","John Mathew","Wonda Kher","Simon Ardino"  
           };  
           //LINQ Query using Method Syntax  
           var MethodSyntax = employeeList.Where(emp => emp.Contains("John"));  
           //Execution  
           foreach (var methoditem in MethodSyntax)  
           {  
             Console.Write(methoditem + " ");  
           }  
           Console.ReadLine();  
         }  
       }  
     }  
Code Explanation
  • The datasource is same as per previous example.
  • We change the query syntax to method syntax with help of lambda expression.
  • select object is used for selection the filter list
After run the application the same output be print "John Thomas" and "John Mathew".
Benefit of use Method Syntax
  • Less code with prominent result.
  • More filter option with little bit enhancement.
  • Lambda Expression makes writing of delegate codes much easier, simpler and readable

Summary
    In this tutorial we discussed how to write Linq Query Syntax. If have any question related to this topic then give your feedback.

Post a Comment

Previous Post Next Post