Language-Integrated Query (LINQ) Queries


Language-Integrated Query (LINQ) is a set of features introduced that extends powerful query capabilities to the language syntax of C#. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store. Visual Studio includes LINQ provider assemblies that enable the use of LINQ with .NET Framework collections, SQL Server databases, ADO.NET Datasets, and XML documents. 

Obtaining a Data Source:

In a LINQ query, we have first priority to obtaining a data source. In C#, as another programming language are follow a culture that, before using variables, we have define first. In a LINQ query, the from clause comes first in order to introduce the data source (Student) and the range variable (stud).

 var allStudent = from stud in _db.Student
 select stud;
**Here, _db is a database.


This is the most common query operation is to apply a filter in the form of a Boolean expression. The filter those records that satisfy the given conditions. The result is produced by using the where clause.. In the following example, we have try to find only those Student who have a Last Name in Mishra are returned.

var LastNameMishra =  from stud in _db.Student
                        where stud.LastName == "Mishra"
                           select stud;

We can use the familiar C# logical AND and OR operators to apply as many filter expressions as necessary in the where clause. For example, to return only those student where city is Delhi from “Student” table  AND whose name is "Anupam" we would write the following code: 

where stud.City=="Delhi" && stud.FirstName == "Anupam"

where stud.City == "Noida" || stud.LastName == "Mishra"

Here, we have sorting data in an ascending or descending order. The orderby clause will cause the elements in the returned sequence to be sorted according to the default comparer for the type being sorted. For example, the following query can be extended to sort the results based on the student name property. Because student name is a string, the default comparer performs an alphabetical sort from A to Z.



var queryNoidaStudent= 
                           from stud in Student
                              where stud.City =="Noida"
                                 orderby stud.Name ascending
                                       select stud;



The group clause enables to group our results based on a key that we specify. For example we could specify that the results should be grouped by the City so that all students from Noida or Delhi are in individual groups. In this case, stud.City is the key.

var queryStudenByCity =
                        from stud in Student
                           group stud by stud.City;
var studQuery =  from stud in Student
                    group stud by cust.City into studGroup
                       where stud.Count() > 2
                          orderby stud.Key
                             select studGroup;


Join operations create associations between sequences that are not explicitly modeled in the data sources. For example we can perform a join to find all the students and instructors who have the same location. In LINQ the join clause always works against object collections instead of database tables directly.

var innerJoinQuery =
    from stud in Student
    join inst in Instructor on stud.City equals inst.City
   select new { StudentName = stud.Name, InstructorName = inst.Name};

Let Clause:

In a query expression, it is sometimes useful to store the result of a sub-expression in order to use it in subsequent clauses. We can do this with the let keyword, which creates a new range variable and initializes it with the result of the expression we supply. Once initialized with a value, the range variable cannot be used to store another value. However, if the range variable holds a queryable type, it can be queried.

string[] str = 
            "Established in 2010 and headquartered in Allahabad, MindStick Software Pvt.Ltd. Our expertise is in software product design, custom programming, database design,web design and programming and cutting edge mobile applications development." };
            // select those whose first letter is a vowel.
            var CheckVowelQuery = 
                from sentence in str
                let words = sentence.Split(' ')
                from word in words
                let w = word.ToLower()
                where w[0] == 'a' || w[0] == 'e'
                   || w[0] == 'i' || w[0] == 'o'
                   || w[0] == 'u'
                select word;


Language-Integrated Query (LINQ) Queries

Last updated:9/7/2019 12:16:14 AM


Manish Kumar
Manish Kumar

Thanks Anupam for providing very nice information about language-integrated query.

Leave Comment