Language Integrated Query (LINQ)

In this blog, I’m explaining the language integrated query and how to use it in our application.

LINQ stands for Language Integrated Query is a modern Microsoft technology. It provides the language level support mechanism to query all types of data.

Language-Integrated Query, or LINQ, is an extension to the .NET Framework in version 3.5 that makes queries and set operations first class citizens of .NET languages such as C#. It has been further defined as, 'A set of general purpose standard query operators that allow traversal, filter, and projection operations to be expressed in a direct yet declarative way in any .NET language

Example                              
Step 1:

Create a windows application and add a textbox and button and datagridview like this:

 

Step 2:

Create a list and add data in it:

public class Student
{
public string Name { get; set; }
public string Address { get; set; }
public string CourseName { get; set; }
public string ContactNumber { get; set; }
}
List<Student> students = new List<Student>();
 
students.Add(new Student { Name = 'Mark', Address = 'NewYork', CourseName = 'M.B.A.', ContactNumber = '05127893628' });
           
students.Add(new Student { Name = 'Steve', Address = 'NewJersy', CourseName = 'M.C.A.', ContactNumber = '0512734890' });
           
students.Add(new Student { Name = 'John', Address = 'LosAngeles', CourseName = 'B.Tech', ContactNumber = '05127812345' });

Step 3:

Write the following linq query to get data in datagridview :

var query = from student in students
where student.Name.ToLower().Contains(searchText) ||
                            student.Address.ToLower().Contains(searchText) ||
                            student.CourseName.ToLower().Contains(searchText) ||
                            student.ContactNumber.ToLower().Contains(searchText)
                           
select student;

The above linq query will give you data that matches with your entered


text.

Full Code:

 

public partial class frmStudentSearch : Form
{
  public class Student
  {
     public string Name { get; set; }
     public string Address { get; set; }
     public string CourseName { get; set; }
     public string ContactNumber { get; set; }
  }
  List<Student> students = new List<Student>();
  public frmStudentSearch()
  {
     InitializeComponent();
  }
private void Form1_Load(object sender, EventArgs e)
{
     students.Add(new Student { Name = 'Mark', Address = 'NewYork', CourseName = 'M.B.A.', ContactNumber = '05127893628' });
           
students.Add(new Student { Name = 'Steve', Address = 'NewJersy', CourseName = 'M.C.A.', ContactNumber = '0512734890' });
 
          students.Add(new Student { Name = 'John', Address = 'LosAngeles', CourseName = 'B.Tech', ContactNumber = '05127812345' });
}
private void btnSearch_Click(object sender, EventArgs e)
{
  string searchText = txtSearch.Text.Trim().ToLower();
  if (!string.IsNullOrEmpty(searchText))
  {
    var query = from student in students
                            where student.Name.ToLower().Contains(searchText) ||
                            student.Address.ToLower().Contains(searchText) ||
                            student.CourseName.ToLower().Contains(searchText) ||
                            student.ContactNumber.ToLower().Contains(searchText)
                           
select student;
dataGridStudents.DataSource = query.ToList();
  }
}
}

Output

 

It will search anything that exists in the list like if you type Name, Address, Course name or Contact Number.

Last updated:9/18/2014 1:24:19 PM

0 Comments

Leave Comment