Quick Answer
Process collections with Language Integrated Query.
Understanding the Issue
LINQ provides SQL-like query capabilities for in-memory and external data with deferred execution.
The Problem
This code demonstrates the issue:
Csharp
Error
List<Person> people = GetPeople();
// Need to query data
The Solution
Here's the corrected code:
Csharp
Fixed
// Filtering
var adults = people.Where(p => p.Age >= 18);
// Projection
var names = people.Select(p => p.Name);
// Sorting
var ordered = people.OrderBy(p => p.Name)
.ThenByDescending(p => p.Age);
// Aggregation
double avgAge = people.Average(p => p.Age);
int oldest = people.Max(p => p.Age);
// Grouping
var byAge = people.GroupBy(p => p.Age / 10 * 10);
// Join
var joined = from p in people
join a in addresses on p.Id equals a.PersonId
select new { p.Name, a.City };
// Query syntax
var query = from p in people
where p.Age > 21
orderby p.Name
select p.Name;
Key Takeaways
Use LINQ for declarative data processing with deferred execution.