top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

What is LINQ in C#?

0 votes
What is LINQ in C#?
posted Apr 17, 2017 by Sunil

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes


LINQ stands for Language Integrated Query. LINQ is a data querying methodology which provides querying capabilities to .NET languages with a syntax similar to a SQL query

LINQ has a great power of querying on any source of data. The data source could be collections of objects, database or XML files. We can easily retrieve data from any object that implements the IEnumerable interface. Microsoft basically divides LINQ into three areas and that are give below.

The official goal of the LINQ family of technologies is to add "general purpose query facilities to the .NET Framework that apply to all sources of information, not just relational or XML data".

enter image description here

Advantages of LINQ

LINQ offers an object-based, language-integrated way to query over data no matter where that data came from. So through LINQ we can query database, XML as well as collections.
Compile time syntax checking
It allows you to query collections like arrays, enumerable classes etc in the native language of your application, like VB or C# in much the same way as you would query a database using SQL
LINQ to Object {Queries performed against the in-memory data}


LINQ to SQL (DLinq) {Queries performed against the relation database only Microsoft SQL Server Supported}
LINQ to DataSet {Supports queries by using ADO.NET data sets and data tables}
LINQ to Entities
LINQ to XML (XLinq) { Queries performed against the XML source}

LINQ to Objects deals with in-memory data. Any class that implements the IEnumerable interface (in the System.Collections.Generic namespace) can be queried with SQO.

LINQ to ADO.NET deals with data from external sources, basically anything ADO.NET can connect to. Any class that implements IEnumerable or IQueryable (in the System.Query namespace) can be queried with SQO.


  int[] nums = new int[] {0,1,2};
      var res = from a in nums where a < 3 orderby a select a;
      foreach(int i in res) 

e.g. ASP.NET


public class patient
      public patient()
           // Fields
        private string _name;
        private int _age;
        private string _gender;
       private string _area;
        // Properties
        public string PatientName
            get { return _name; }
            set { _name = value; }
         public string Area
            get { return _area; }
            set { _area = value; }
        public String Gender
            get { return _gender; }
            set { _gender = value; }
        public int Age
            get { return _age; }
            set { _age = value; }

Main Program

using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e)
        List<patient> pat=new List<patient>();
        patient p=new patient();
        p.patientname="Deepak dwij";
        p.patientstate = "UP";
        p.patientage = "25";
        p.patientcity = "Noida";
        GridView1.DataSource = from a in pat select a;
        //GridView1.DataSource = from pa in patients
                                   where pa.Gender == "Male"
                                   orderby pa.PatientName, pa.Gender, pa.Age
                                   select pa;


The following code uses the selection operator type, which brings all those records whose age is more than 20 years.

var mypatient = from pa in patients
                  where pa.Age > 20
                  orderby pa.PatientName, pa.Gender, pa.Age
                  select pa;
        foreach(var pp in mypatient)
        Debug.WriteLine(pp.PatientName + " "+ pp.Age + " " + pp.Gender);


The following code snippet uses the grouping operator type that group patient data on the bases area.

var op = from pa in patients
            group pa by pa.Area into g
            select new {area = g.Key, count = g.Count(), allpatient = g};
    foreach(var g in op)
      Debug.WriteLine(g.count+ "," + g.area);
       foreach(var l in g.allpatient)


int patientCount = (from pa in patients
                    where pa.Age > 20
                    orderby pa.PatientName, pa.Gender, pa.Age 
                   select pa).Count();

Linq Example

Simple select

int[] numbers = { 5, 4, 1, 3, 9, 8};
        var numsPlusOne =from n in numbers select n;
        foreach (var i in numsPlusOne)
            MessageBox.Show(i.ToString() );

Multiple select

 int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
int[] numbersB = { 1, 3, 5, 7, 8 };
var pairs =from a in numbersA from b in numbersB where a < b select new { a, b };
Console.WriteLine("Pairs where a < b:");
foreach (var pair in pairs)
    Console.WriteLine("{0} is less than {1}", pair.a, pair.b);

Order by

string[] words = { "cherry", "apple", "blueberry" };
var sortedWords =from w in words orderby w select w;
Console.WriteLine("The sorted list of words:");
foreach (var w in sortedWords)

Count function

 int[] factorsOf300 = { 2, 2, 3, 5, 5 };
  int uniqueFactors = factorsOf300.Distinct().Count();
   Console.WriteLine("There are {0} unique factors of 300.", uniqueFactors);


int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
  int oddNumbers = numbers.Count(n => n % 2 == 1);
   Console.WriteLine("There are {0} odd numbers in the list.", oddNumbers);
answer Apr 19, 2017 by Shivaranjini
Similar Questions
+2 votes

Develop a C# Windows Form Application that allows users to do all of the following.
Read a List of patient's information from a text file (*.txt), the input text file is selected from the Open File Dialog.
Your program should accept any text file with the following format:
a. The file contains columns with Basic information about patients.
b. Columns may be separated by spaces and/or tabs.
c. The first line in the file is the column header.
d. After the header, each line represents a Patient (name, address, phone#, Bdate, gander ,wheight, height and blood type).
e. Successfully imported patients are displayed on a data grid view in a second form and added to a patient list.

  1. The user will be able to display on The Grid view :
    a) Female patients.
    b) Patients with age<45 year. c) Save Over weighted patients on a text file .  Note: To find over weighted patients you have to calculate the BMI value. BMI=Weight/ (Height*Height). If BMI <18.5 >>>>> under weighted.
    18.5<=BMI<=25 >>>>>>>Normal.
    BMI>25 >>>>>>>>>>>> Over Weighted.
Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.