×

iFour Logo

Using LINQ in MVC .NET Core

Kapil Panchal - December 02, 2020

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

  • play
  • pause
  • pause
Using LINQ in MVC .NET Core

LINQ stands for Language Integrated Query. Language Integrated Query is one structured query that is used to retrieve data from the database and other different sources and formats.

LINQ tutorials will assist you to find out the LINQ language using topics that go from basic to advanced. These tutorials are weakened into a series of related topics, in order that you begin from a subject that has got to be understood first, then gradually learn other features of LINQ sequentially. using language integrated query, you can easily retrieve data from your database and do other operations like insert update and delete it is easy in LINQ.

LINQ has some in-built functions using which you can apply different conditions easily and write less than SQL query. In this blog you industrious about some of them and their works. It is also providing a single querying interface for different types of data sources.

For example, Structure Query Language (SQL) is used to retrieve data and save data from the database. Just like it, LINQ is also a structured query which is used to retrieve data from different types of data sources like XML Docs, WEB Service and MS SQL Server, and other Databases.

Why we use LINQ?

To understand why should we use LINQ, let’s take some examples. Like if you want to find some employee whose salary is below 20000 from an array of Employee object.

We use “for each” or “For” Loop to traverse the list or collection to find a particular object. Suppose we had to write the below code to find all employee from an array of employee array where the salary is below 20000 (For finding below 20000 salary Employees):

Example: Use for Each loop to find elements.

 
class Employee  
{
public int EmployeeID {get; set;}
public String EmployeeName {get; set;}
public int Salary {get; set;}
}
class MainProgram
{
static void Main (string [] args)
{
Employee [] employeeArray = {
new Employee() { EmployeeID = 1, EmployeeName = "Harshad", Salary= 18000 },
new Employee() { EmployeeID = 2, EmployeeName = "Darshak",  Salary= 21000 },
new Employee() { EmployeeID = 3, EmployeeName = "Saurabh",  Salary= 25000 },
new Employee() { EmployeeID = 4, EmployeeName = "Smeet" , Salary= 15000},
new Employee() { EmployeeID = 5, EmployeeName = "Chirag" , Salary= 31000 },
new Employee() { EmployeeID = 6, EmployeeName = "Tarun",  Age = 17 },
new Employee() { EmployeeID = 7, EmployeeName = "Prahlad",Salary= 35000},
};
Employee[] Employee= new Employee[10];
int i = 0;
foreach (Employee emp in employeeArray)
{
if(emp.Salary<20000)
{
Employee[i] = emp;
i++;
}
}
}
}

               

 

The use of for loop is unmanageable, it is not maintainable and readable. For this problem, C# 2.0 introduced us delegate, which is used to handle this kind of a problem, as shown below:

Example: Using Delegates find an element from the collection in C#2.0

 
delegate bool FindEmployee(Employee emp);
class EmployeeExtension
{
public static Employee[] where(Employee[] empArray, FindEmployee del)
{
int i=0;
Employee[] result = new Employee[10];
foreach (Employee emp in empArray)
if (del(emp))
{
result[i] = emp;
i++;
}
return result;
}
}
class MainProgram
{
static void Main(string[] args)
{
Employee[] employeeArray = {
new Employee() { EmployeeID = 1, EmployeeName = "Harshad", Salary= 18000 },
new Employee() { EmployeeID = 2, EmployeeName = "Darshak",  Salary= 21000 },
new Employee() { EmployeeID = 3, EmployeeName = "Saurabh",  Salary= 25000 },
new Employee() { EmployeeID = 4, EmployeeName = "Smeet" , Salary= 15000},
new Employee() { EmployeeID = 5, EmployeeName = "Chirag" , Salary= 31000 },
new Employee() { EmployeeID = 6, EmployeeName = "Tarun",  Age = 17 },
new Employee() { EmployeeID = 7, EmployeeName = "Prahlad",Salary= 35000},
};
Employee[]students=EmployeeExtension.where(employeeArray, delegate(Employee emp){
return emp.Salary<20000;
});
}
}
}

Now, you can have the advantage of the delegate for finding employees with criteria. you don’t need to use the “ForEach” loop to find employees using different conditions. For example, you can use this same delegate to find a specific EmployeeID Employee or Within Specific Name of Employee.

 
Employee [] Employees= EmployeeExtension.where(employeeArray, delegate (Employee emp) {
return emp.EmployeeID == 3;
});
//Also, use another conditions using same delegate
Employee[] Employees= EmployeeExtension.where(employeeArray, delegate(Employee emp) {
return emp.Name== “Prahlad”;
});


C#team still needs to make code smaller and under-stable. So, they introduced us with the extension method, lambda expression and, anonymous type and expression tree, and query expression in C#3.0, which are building blocks of LINQ to query to the different type of collection like Database, List, Array, Etc. And get the resulted data in a single object or statement.

Searching for Dedicated ASP.Net Core Web Developer ? Your Search ends here.

 

The below example shows how you can use the LINQ query with a lambda expression to find a specific employee€ from the employee collection. Below example is the LINQ example

 
class MainProgram
{
static void Main(string[] args)
{
Employee[] employeeArray = {
new Employee() { EmployeeID = 1, EmployeeName = "Harshad", Salary= 18000 },
new Employee() { EmployeeID = 2, EmployeeName = "Darshak",  Salary= 21000 },
new Employee() { EmployeeID = 3, EmployeeName = "Saurabh",  Salary= 25000 },
new Employee() { EmployeeID = 4, EmployeeName = "Smeet" , Salary= 15000},
new Employee() { EmployeeID = 5, EmployeeName = "Chirag" , Salary= 31000 },
new Employee() { EmployeeID = 6, EmployeeName = "Tarun",  Age = 17 },
new Employee() { EmployeeID = 7, EmployeeName = "Prahlad",Salary= 35000},
};
// Use LINQ to find bellow 20000 salary employee
Employee[] employees= employeeArray.Where(s => s.Salary <20000).ToArray();
// Use LINQ to find the first employee whose name is Saurabh
Employee[] employees= employeeArray.Where(s => s.EmployeeName ==”Saurabh”).ToArray();
// Use LINQ to find an Employee whose EmployeeID is 5
Employee[] employees= employeeArray.Where(s => s.EmployeeID == 2).ToArray();
}
}

As you see above example we specify different conditions using the LINQ operator and lambda expression in a single line or expression. LINQ is making code more readable and it is also used with different data sources. For example, list, array, database, etc. you can also find employees using Entity Framework with the same query.

Conclusion

We hope from this blog you understand what is LINQ and how to use it. LINQ is like a SQL but have more features and inbuilt functions using which you can write code more easily and more readable and apply conditions to retrieve data from different objects.

Using LINQ in MVC .NET Core LINQ stands for Language Integrated Query. Language Integrated Query is one structured query that is used to retrieve data from the database and other different sources and formats. LINQ tutorials will assist you to find out the LINQ language using topics that go from basic to advanced. These tutorials are weakened into a series of related topics, in order that you begin from a subject that has got to be understood first, then gradually learn other features of LINQ sequentially. using language integrated query, you can easily retrieve data from your database and do other operations like insert update and delete it is easy in LINQ. LINQ has some in-built functions using which you can apply different conditions easily and write less than SQL query. In this blog you industrious about some of them and their works. It is also providing a single querying interface for different types of data sources. For example, Structure Query Language (SQL) is used to retrieve data and save data from the database. Just like it, LINQ is also a structured query which is used to retrieve data from different types of data sources like XML Docs, WEB Service and MS SQL Server, and other Databases. Why we use LINQ? To understand why should we use LINQ, let’s take some examples. Like if you want to find some employee whose salary is below 20000 from an array of Employee object. We use “for each” or “For” Loop to traverse the list or collection to find a particular object. Suppose we had to write the below code to find all employee from an array of employee array where the salary is below 20000 (For finding below 20000 salary Employees): Example: Use for Each loop to find elements.   class Employee { public int EmployeeID {get; set;} public String EmployeeName {get; set;} public int Salary {get; set;} } class MainProgram { static void Main (string [] args) { Employee [] employeeArray = { new Employee() { EmployeeID = 1, EmployeeName = "Harshad", Salary= 18000 }, new Employee() { EmployeeID = 2, EmployeeName = "Darshak", Salary= 21000 }, new Employee() { EmployeeID = 3, EmployeeName = "Saurabh", Salary= 25000 }, new Employee() { EmployeeID = 4, EmployeeName = "Smeet" , Salary= 15000}, new Employee() { EmployeeID = 5, EmployeeName = "Chirag" , Salary= 31000 }, new Employee() { EmployeeID = 6, EmployeeName = "Tarun", Age = 17 }, new Employee() { EmployeeID = 7, EmployeeName = "Prahlad",Salary= 35000}, }; Employee[] Employee= new Employee[10]; int i = 0; foreach (Employee emp in employeeArray) { if(emp.Salary<20000) { Employee[i] = emp; i++; } } } } Read More: What Is Routing In Asp.Net Core Mvc?   The use of for loop is unmanageable, it is not maintainable and readable. For this problem, C# 2.0 introduced us delegate, which is used to handle this kind of a problem, as shown below: Example: Using Delegates find an element from the collection in C#2.0   delegate bool FindEmployee(Employee emp); class EmployeeExtension { public static Employee[] where(Employee[] empArray, FindEmployee del) { int i=0; Employee[] result = new Employee[10]; foreach (Employee emp in empArray) if (del(emp)) { result[i] = emp; i++; } return result; } } class MainProgram { static void Main(string[] args) { Employee[] employeeArray = { new Employee() { EmployeeID = 1, EmployeeName = "Harshad", Salary= 18000 }, new Employee() { EmployeeID = 2, EmployeeName = "Darshak", Salary= 21000 }, new Employee() { EmployeeID = 3, EmployeeName = "Saurabh", Salary= 25000 }, new Employee() { EmployeeID = 4, EmployeeName = "Smeet" , Salary= 15000}, new Employee() { EmployeeID = 5, EmployeeName = "Chirag" , Salary= 31000 }, new Employee() { EmployeeID = 6, EmployeeName = "Tarun", Age = 17 }, new Employee() { EmployeeID = 7, EmployeeName = "Prahlad",Salary= 35000}, }; Employee[]students=EmployeeExtension.where(employeeArray, delegate(Employee emp){ return emp.Salary<20000; }); } } } Now, you can have the advantage of the delegate for finding employees with criteria. you don’t need to use the “ForEach” loop to find employees using different conditions. For example, you can use this same delegate to find a specific EmployeeID Employee or Within Specific Name of Employee.   Employee [] Employees= EmployeeExtension.where(employeeArray, delegate (Employee emp) { return emp.EmployeeID == 3; }); //Also, use another conditions using same delegate Employee[] Employees= EmployeeExtension.where(employeeArray, delegate(Employee emp) { return emp.Name== “Prahlad”; }); C#team still needs to make code smaller and under-stable. So, they introduced us with the extension method, lambda expression and, anonymous type and expression tree, and query expression in C#3.0, which are building blocks of LINQ to query to the different type of collection like Database, List, Array, Etc. And get the resulted data in a single object or statement. Searching for Dedicated ASP.Net Core Web Developer ? Your Search ends here. See here   The below example shows how you can use the LINQ query with a lambda expression to find a specific employee€ from the employee collection. Below example is the LINQ example   class MainProgram { static void Main(string[] args) { Employee[] employeeArray = { new Employee() { EmployeeID = 1, EmployeeName = "Harshad", Salary= 18000 }, new Employee() { EmployeeID = 2, EmployeeName = "Darshak", Salary= 21000 }, new Employee() { EmployeeID = 3, EmployeeName = "Saurabh", Salary= 25000 }, new Employee() { EmployeeID = 4, EmployeeName = "Smeet" , Salary= 15000}, new Employee() { EmployeeID = 5, EmployeeName = "Chirag" , Salary= 31000 }, new Employee() { EmployeeID = 6, EmployeeName = "Tarun", Age = 17 }, new Employee() { EmployeeID = 7, EmployeeName = "Prahlad",Salary= 35000}, }; // Use LINQ to find bellow 20000 salary employee Employee[] employees= employeeArray.Where(s => s.Salary <20000).ToArray(); // Use LINQ to find the first employee whose name is Saurabh Employee[] employees= employeeArray.Where(s => s.EmployeeName ==”Saurabh”).ToArray(); // Use LINQ to find an Employee whose EmployeeID is 5 Employee[] employees= employeeArray.Where(s => s.EmployeeID == 2).ToArray(); } } As you see above example we specify different conditions using the LINQ operator and lambda expression in a single line or expression. LINQ is making code more readable and it is also used with different data sources. For example, list, array, database, etc. you can also find employees using Entity Framework with the same query. Read More: .NET Core 6 improvements in LINQ: An essential guide for 2023 Conclusion We hope from this blog you understand what is LINQ and how to use it. LINQ is like a SQL but have more features and inbuilt functions using which you can write code more easily and more readable and apply conditions to retrieve data from different objects.
Kapil Panchal

Kapil Panchal

A passionate Technical writer and an SEO freak working as a Content Development Manager at iFour Technolab, USA. With extensive experience in IT, Services, and Product sectors, I relish writing about technology and love sharing exceptional insights on various platforms. I believe in constant learning and am passionate about being better every day.

Build Your Agile Team

Enter your e-mail address Please enter valid e-mail

Categories

Ensure your sustainable growth with our team

Talk to our experts
Sustainable
Sustainable
 
Blog Our insights
How Spatial Data Analysis Improves Healthcare
How Spatial Data Analysis Improves Healthcare

Do you know when geospatial analysis took traction in healthcare? It was when John Snow, a London-based physician, used it to analyze the spread of cholera, which ultimately proved...

4 Types of Power BI Dashboards: Analytical, Strategic, Operational, and Tactical
4 Types of Power BI Dashboards: Analytical, Strategic, Operational, and Tactical

One interesting aspect you truly love about Power BI, as a CTO, is how it lets you step back and see the bigger picture of your business. Isn’t it? Without getting bogged down in minute...

Power Apps Integration Challenges: Expert Insights & Solutions
Power Apps Integration Challenges: Expert Insights & Solutions

Power Apps Integration Challenges: Insights from 11 Experts Microsoft Power Apps has caught great hype in recent days. Why? because it helps you build apps 50% faster, saves 250+...