Mid Collections

How can you perform a LINQ query on a collection to filter, sort, or group elements?

  • Filter: Use Where() to select elements based on a condition.
  • Sort: Use OrderBy() or OrderByDescending().
  • Group: Use GroupBy() to group elements by a key.

Example:

var products = new List<Product> { ... };

// Filter products with price > 100

var expensiveProducts = products.Where(p => p.Price > 100);

// Sort products by name

var sortedProducts = products.OrderBy(p => p.Name);

// Group products by category

var groupedProducts = products.GroupBy(p => p.Category);

Follow:

More from C# Programming Tutorial

All questions for this course