Introduction:

In this article we are going to explain how to use LINQ in project or how to write a LINQ Query using C# with example.

Description:

 LINQ Query Syntax:

LINQ is a “Language Integrated Query” and its main feature is to allows users to write SQL style of queries with in code using query syntax’s.

.NET framework (3.5 or Higher) provides set of built in query keywords in LINQ to allow users to write SQL style of queries with in code.

There are two ways in which we can write queries in LINQ

1. Using Query Syntax

2. Using Method Syntax


LINQ Query Syntax in C#

 

using System;
using System.Collections.Generic;
using System.Linq;
namespace SQLTutorialsPoint
{
    class Program
    {
        static void Main(string[] args)
        {
            //string collection
            IList<string> stringList = new List<string>() {
                                            "C# Tutorials",
                                            "VB.NET Tutorials",
                                            "Learn C++",
                                            "MVC Tutorials" ,
                                            "Java"
                                            };
 
            // LINQ Query Syntax
            var result = from s in stringList
                         where s.Contains("Tutorials")
                         select s;
 
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
 
            Console.ReadLine();
        }
    }
}

When we execute above c# program, we will get the result like as shown below.

 LINQ Query Syntax And Method Syntax

 

As you see above example. We have a string array having five elements. We will print the elements from given array where element value contains tutorials using LINQ.

 

LINQ Method Syntax in C# with collection

using System;
using System.Collections.Generic;
using System.Linq;
namespace SQLTutorialsPoint
{
    class Program
    {
        class Student
        {
            public int StudentID { get; set; }
            public string StudentName { get; set; }
            public int Age { get; set; }
        }
        static void Main(string[] args)
        {
            // Student collection
         IList<Student> studentList = new List<Student>() {
        new Student() { StudentID = 1, StudentName = "Aditya", Age =13} ,
        new Student() { StudentID = 2, StudentName = "Pawan",  Age = 21 } ,
        new Student() { StudentID = 3, StudentName = "Rohit",  Age = 18 } ,
        new Student() { StudentID = 4, StudentName = "mayank" , Age =20} ,
        new Student() { StudentID = 5, StudentName = "Sanat" , Age =15 }
    };
 
        // LINQ Query Method Syntax to find out students
         var result = studentList.Where(x => x.Age > 12 && x.Age < 20).ToList();                      
            foreach (var item in result)
            {
                Console.WriteLine("Name :"+item.StudentName + "Age :"+item.Age);
            }
            Console.ReadLine();
        }
    }
}

 

When we execute above c# program, we will get the result like as shown below.

 LINQ Query Syntax And Method Syntax

As you see above example. We have a student collection having five students. We will print from student collection between student age 12 to 25 using LINQ where method and ToList().


I hope it will help to you after reading.

comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like