LINQ in C#: Query & Method Syntax, Filtering, Sorting, Grouping & Best Practices

1. LINQ (Language Integrated Query)

Q: What is LINQ in C#?

LINQ (Language Integrated Query) is a set of C# features that enables querying data (e.g., collections, databases, XML) using a unified, SQL-like syntax. Introduced in C# 3.0, LINQ is part of the .NET framework (System.Linq) and works with any data source implementing IEnumerable<T> or IQueryable<T>. It supports querying in a type-safe, expressive way.

Q: What are the benefits of LINQ?

Q: How does LINQ differ from C/C++?

2. Query Syntax and Method Syntax

Q: What is Query Syntax in LINQ?

Query syntax is a declarative, SQL-like syntax for writing LINQ queries, using keywords like from, where, select, orderby, and group. It is more readable for complex queries and resembles SQL.

Syntax:

from variable in source
where condition
select result

Q: What is Method Syntax in LINQ?

Method syntax uses extension methods (e.g., Where, Select, OrderBy) defined in System.Linq to chain operations on collections. It is more programmatic and often more concise for simple queries.

Syntax:

source.Where(condition).Select(result)

Q: What are the differences between Query Syntax and Method Syntax?

Q: Can you give an example comparing Query Syntax and Method Syntax?

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqSyntax
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };

            // Query Syntax: Filter even numbers
            var querySyntax = from num in numbers
                             where num % 2 == 0
                             select num;
            Console.WriteLine("Query Syntax (Even numbers):");
            foreach (var num in querySyntax)
            {
                Console.Write($"{num} ");
            }

            // Method Syntax: Filter even numbers
            var methodSyntax = numbers.Where(n => n % 2 == 0);
            Console.WriteLine("\nMethod Syntax (Even numbers):");
            foreach (var num in methodSyntax)
            {
                Console.Write($"{num} ");
            }
        }
    }
}

Output:

Query Syntax (Even numbers):
2 4 6
Method Syntax (Even numbers):
2 4 6

3. Filtering, Sorting, and Grouping

Q: What is filtering in LINQ?

Filtering selects elements from a collection that satisfy a condition, using the where clause (query syntax) or Where method (method syntax).

Example:

// Query: where num > 3
// Method: numbers.Where(n => n > 3)

Q: What is sorting in LINQ?

Sorting orders elements based on a key, using orderby (query syntax) or OrderBy/OrderByDescending (method syntax). Multiple keys can be sorted with thenby/ThenBy.

Example:

// Query: orderby name ascending
// Method: names.OrderBy(n => n)

Q: What is grouping in LINQ?

Grouping organizes elements into groups based on a key, using group by (query syntax) or GroupBy (method syntax). Each group contains a key and a collection of matching elements.

Example:

// Query: group by category
// Method: items.GroupBy(i => i.Category)

Q: Can you give an example of filtering, sorting, and grouping in LINQ?

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqOperations
{
    class Product
    {
        public string Name { get; set; }
        public string Category { get; set; }
        public decimal Price { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Product> products = new List<Product>
            {
                new Product { Name = "Laptop", Category = "Electronics", Price = 999.99m },
                new Product { Name = "Phone", Category = "Electronics", Price = 699.99m },
                new Product { Name = "Shirt", Category = "Clothing", Price = 29.99m },
                new Product { Name = "Jeans", Category = "Clothing", Price = 49.99m }
            };

            // Query Syntax: Filtering, Sorting, Grouping
            var querySyntax = from product in products
                             where product.Price > 50
                             orderby product.Price descending
                             group product by product.Category into g
                             select new
                             {
                                 Category = g.Key,
                                 Products = g
                             };
            Console.WriteLine("Query Syntax (Filtering, Sorting, Grouping):");
            foreach (var group in querySyntax)
            {
                Console.WriteLine($"Category: {group.Category}");
                foreach (var product in group.Products)
                {
                    Console.WriteLine($"  {product.Name}: {product.Price:C}");
                }
            }

            // Method Syntax: Filtering, Sorting, Grouping
            var methodSyntax = products
                .Where(p => p.Price > 50)
                .OrderByDescending(p => p.Price)
                .GroupBy(p => p.Category)
                .Select(g => new { Category = g.Key, Products = g });
            Console.WriteLine("\nMethod Syntax (Filtering, Sorting, Grouping):");
            foreach (var group in methodSyntax)
            {
                Console.WriteLine($"Category: {group.Category}");
                foreach (var product in group.Products)
                {
                    Console.WriteLine($"  {product.Name}: {product.Price:C}");
                }
            }
        }
    }
}

Output:

Query Syntax (Filtering, Sorting, Grouping):
Category: Electronics
  Laptop: $999.99
  Phone: $699.99

Method Syntax (Filtering, Sorting, Grouping):
Category: Electronics
  Laptop: $999.99
  Phone: $699.99

Q: How do filtering, sorting, and grouping in LINQ differ from C/C++?

Q: What are common mistakes with LINQ in C#?

Query/Method Syntax:

Filtering:

Sorting:

Grouping:

General:

Q: What are best practices for LINQ in C#?

Query/Method Syntax:

Filtering:

Sorting:

Grouping:

General: