Exploring LINQ - SkillBakery Studios

Breaking

Post Top Ad

Post Top Ad

Monday, September 14, 2020

Exploring LINQ

LINQ is uniform query syntax in C# & VB.Net to retrieve data from different sources and formats.Integrated in C#, thereby eliminating the mismatch between programming launguages and databases.

For eg  In SQL , SQL is used to save & retrieve data from a database using tables same in LINQ ,it is used to retrieve data from different types and different data sources such as ADO.NET dataset, XML Docs ,Web services, MS SQL Server & Other databases.

 

Why LINQ &What is LINQ?

 

 


Queries through LINQ returns result as objects.It uses the object oriented approach on the result set.

 For E.g

LINQ query to retrieve an array

//Data source

String[] names= {“Bhawna”,” Anoop”,” Kunwar”, “Kiyansh”};

//LINQ Query

Var myLinqQuery = from name in names

                                   Where name.contains(‘h’)

                                     Select name;

//Execution

Foreach(var name in myLinqQuery)

Console.write(name+ “”);

In this example we are using foreach loop to execute our query stored in myLinqQuery.foreach loop executes the query on the data source & get the result.

 Why to use LINQ?

Let's take an example where we have to find top scorer students from an array of student objects. Previously when we have to do such a query we find all students' objects from an array of students where marks are bet 90 and 100.

class Student

{

    public int StudentID { get; set; }

    public String StudentName { get; set; }

    public int Marks { get; set; }

}

 class Program

{

    static void Main(string[] args)

    {

        Student[] studentArray = {

            new Student() { StudentID = 01, StudentName = "Bhawna", Marks = 80 },

            new Student() { StudentID = 02, StudentName = "Anoop",  Marks = 94},

            new Student() { StudentID = 03, StudentName = "Kunwar",  Marks = 95 },

            new Student() { StudentID = 04, StudentName = "Kiyansh" , Marks = 90 },

            new Student() { StudentID = 05, StudentName = "Monu" , Marks = 91 },

        };

 

        Student[] students = new Student[10];

 

        int i = 0;

 

        foreach (Student stu in studentArray)

        {

            if (stu.Marks > 90 && stu.Marks < 100)

            {

                students[i] = stu;

                i++;

            }

        }

    }

}

 

C# team felt to make the code more compact & readable so they introduced the extension method, lambda expression, expression tree.These are building blocks of LINQ to query to the different types of collection & get the desired result in an single statement.

 

 

 

 

Same query when we do using LINQ

Example of LINQ

 

class Program
{
    static void Main(string[] args)
    {
        Student[] studentArray = { 
                    new Student() { StudentID = 01, StudentName = " Bhawna ",Marks = 80 } ,
                    new Student() { StudentID = 02, StudentName = " Anoop ", Marks = 94} ,
                    new Student() { StudentID = 03, StudentName = " Kunwar ", Marks = 95 } ,
                    new Student() { StudentID = 04, StudentName = " Kiyansh " ,Marks = 92 } ,
                    new Student() { StudentID = 05, StudentName = " Monu " ,Marks = 91} ,
                };
 
        // Use LINQ to find top students
        Student[] topStudents = studentArray.Where(s => s.marks > 90 && s.marks < 100).ToArray();
       
       // Use LINQ to find student whose StudentID is 05
        Student student5 = studentArray.Where(s => s.StudentID == 05).FirstOrDefault();
    }
}

 

Advantages of LINQ

Familiar Language – You don’t have to learn new query language for each type of data.

Less Coding – It reduces the amount of code to be written as compared with traditional approach.

Readable Code- It makes code more readable so other developer can easily understand.

Compile Time Safety – Provides type checking of objects at compile time.

Intellisence Support – Provides Intellisense for generic collection

Shaping Data- you can retrieve data in different shapes.

For More interesting and different topics log on to our website

www.skillbakery.com


No comments:

Post a Comment

Post Top Ad