Ad Code

✨🎆 Codex 1.0 PLACEMENT READY PROGRAM! 🎆✨

Get 75% Discount Early bird offer CLICK to JOIN CodeX 1.0 click

LINQ to ASP.NET CORE MVC by Shiva Sir

✅ LINQ in C# – Complete In-Depth Guide with Examples

LINQ is one of the most powerful features of C# that every .NET developer must learn. In this tutorial, you will understand LINQ from basics to advanced with arrays, collections, ASP.NET Core MVC examples, and Join operations.


🔷 What is LINQ?

LINQ (Language Integrated Query) is a .NET feature that allows you to query data directly inside C# code using SQL-like or method-chaining syntax.

LINQ can be used to query:

  • ✅ Arrays
  • ✅ Collections (List, Dictionary, etc.)
  • ✅ Databases (Entity Framework)
  • ✅ XML
  • ✅ JSON

🔷 Types of LINQ Syntax

  • Query Syntax (SQL-like)
  • Method Syntax (Fluent / Chaining)
  • Mixed Syntax (Less Common)

✅ Why LINQ?

❌ Without LINQ

foreach (var item in list)
{
    if (item > 10)
        result.Add(item);
}

✅ With LINQ

var result = list.Where(x => x > 10);
  • ✅ Clean Code
  • ✅ Less Lines of Code
  • ✅ Readable
  • ✅ Powerful

🔹 LINQ Basics

✅ Import Namespace

using System.Linq;

🔹 LINQ Syntax Examples

1️⃣ Query Syntax (SQL-like)

var result = from n in numbers
             where n > 5
             select n;

2️⃣ Method Syntax (Recommended)

var result = numbers.Where(n => n > 5);

✅ Method syntax is preferred in real-world projects


🔹 LINQ with Arrays

Example 1: Filter Even Numbers

int[] numbers = { 1, 2, 3, 4, 5, 6, 7 };

var evenNumbers = numbers.Where(n => n % 2 == 0);

foreach (var n in evenNumbers)
{
    Console.WriteLine(n);
}

Output: 2, 4, 6

Example 2: Select / Projection

var squares = numbers.Select(n => n * n);

Example 3: Order By

var sortedDesc = numbers.OrderByDescending(n => n);

Example 4: Aggregation Functions

numbers.Sum();
numbers.Max();
numbers.Min();
numbers.Average();
numbers.Count();

🔹 LINQ with Collections (List<T>)

Employee Model

class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Department { get; set; }
    public int Salary { get; set; }
}

Sample Data

List<Employee> employees = new List<Employee>
{
    new Employee { Id=1, Name="Amit", Department="IT", Salary=40000 },
    new Employee { Id=2, Name="Neha", Department="HR", Salary=30000 },
    new Employee { Id=3, Name="Rahul", Department="IT", Salary=50000 },
    new Employee { Id=4, Name="Pooja", Department="Finance", Salary=45000 }
};

Example: Where (Filter)

var itEmployees = employees.Where(e => e.Department == "IT");

Output: Amit, Rahul


OrderBy + ThenBy

var sortedEmployees = employees
    .OrderBy(e => e.Department)
    .ThenByDescending(e => e.Salary);

🔹 Advanced LINQ Operators

First / FirstOrDefault

employees.First(e => e.Department == "HR");
employees.FirstOrDefault(e => e.Department == "Admin");

Single / SingleOrDefault

employees.Single(e => e.Id == 1);

Any / All

employees.Any(e => e.Salary > 60000);
employees.All(e => e.Department == "IT");

🔹 GroupBy Example

var groupByDept = employees.GroupBy(e => e.Department);

🔷 LINQ Join Example

Query Syntax

var result = from s in students
             join c in courses
             on s.CourseId equals c.Id
             select new
             {
                 StudentName = s.Name,
                 CourseName = c.CourseName,
                 Marks = s.Marks
             };

Method Syntax

var result = students.Join(courses,
    s => s.CourseId,
    c => c.Id,
    (s, c) => new
    {
        StudentName = s.Name,
        CourseName = c.CourseName,
        Marks = s.Marks
    });

✅ Conclusion

LINQ is a must-have skill for C#, ASP.NET, and ASP.NET Core developers. If you master Where, Select, GroupBy, and Join, you are already interview-ready.

🚀 Want more LINQ tutorials, interview questions, or real-world projects? Just ask!

Post a Comment

0 Comments