How to Use LINQ with Objects in C# - Simple Guide
You can use
LINQ in C# to query collections of objects by writing queries with from, where, and select keywords or using method syntax like .Where() and .Select(). LINQ lets you filter, sort, and transform object lists in a readable and concise way.Syntax
LINQ queries can be written in two main styles: query syntax and method syntax.
- Query syntax: Uses keywords like
from,where, andselectto form readable queries. - Method syntax: Uses extension methods like
.Where(),.Select(), and.OrderBy()on collections.
Both styles work on collections of objects that implement IEnumerable<T>.
csharp
var result = from obj in objects where obj.Property == value select obj;
Example
This example shows how to use LINQ to filter and select objects from a list of Person objects by age.
csharp
using System; using System.Collections.Generic; using System.Linq; public class Person { public string Name { get; set; } public int Age { get; set; } } public class Program { public static void Main() { List<Person> people = new List<Person> { new Person { Name = "Alice", Age = 30 }, new Person { Name = "Bob", Age = 20 }, new Person { Name = "Charlie", Age = 25 } }; // Query syntax var adultsQuery = from person in people where person.Age >= 21 select person.Name; // Method syntax var adultsMethod = people.Where(p => p.Age >= 21).Select(p => p.Name); Console.WriteLine("Adults using query syntax:"); foreach (var name in adultsQuery) { Console.WriteLine(name); } Console.WriteLine("Adults using method syntax:"); foreach (var name in adultsMethod) { Console.WriteLine(name); } } }
Output
Adults using query syntax:
Alice
Charlie
Adults using method syntax:
Alice
Charlie
Common Pitfalls
Some common mistakes when using LINQ with objects include:
- Forgetting to include
using System.Linq;which enables LINQ methods. - Trying to query on null collections, which causes exceptions.
- Confusing query syntax and method syntax mixing without proper parentheses.
- Not understanding deferred execution, so queries run only when enumerated.
csharp
/* Wrong: Missing using directive */ // var adults = people.Where(p => p.Age >= 21); /* Right: Include using System.Linq; */ using System.Linq; var adults = people.Where(p => p.Age >= 21);
Quick Reference
| LINQ Keyword/Method | Purpose | Example |
|---|---|---|
| from | Start a query over a collection | from p in people |
| where | Filter items by condition | where p.Age > 20 |
| select | Choose what to return | select p.Name |
| .Where() | Filter items using a lambda | people.Where(p => p.Age > 20) |
| .Select() | Project items to new form | people.Select(p => p.Name) |
| .OrderBy() | Sort items ascending | people.OrderBy(p => p.Name) |
Key Takeaways
Use LINQ query or method syntax to filter and select objects from collections.
Always include 'using System.Linq;' to access LINQ methods.
LINQ queries run when you enumerate the results, not when you write them.
Be careful to avoid null collections to prevent runtime errors.
LINQ makes object collection queries readable and concise.