Filter
Takes a larger set down to a smaller setFirst
-
customers.First(c => c.LastName == "Anderson")
The first customer with a last name of Anderson
-
customers.Last(c => c.LastName == "Anderson")
The last customer with a last name of Anderson
-
customers.Single(c => c.Id == 456)
The only customer with an ID of 456
-
customers.Where(c => c.Active == true)
All customers that are Active
-
customers.FirstOrDefault(c => c.LastName == "Anderson")
The first customer with a last name of Anderson or null if there isn't one
-
customers.LastOrDefault(c => c.LastName == "Anderson")
The last customer with a last name of Anderson or null if there isn't one
-
customers.SingleOrDefault(c => c.Id = 999)
The only customer with an ID of 999 or null if there isn't one
Sort
Returns a copy of a set but in a different orderOrderBy
-
customers.OrderBy(c => c.LastName)
Returns the customers sorted alphabetically by last name
-
customers.OrderByDescending(c => c.LastOrderDate)
Returns the customers sorted by the date they last placed an order (most recent first)
-
customers.OrderBy(c => c.LastName).ThenBy(c => c.FirstName)
Returns the customers sorted first by last name then by first name
-
customers.Order(c => c.Category).ThenByDescending(c => c.LastOrderDate)
Returns the customers sorted first by category then by the date they last placed an order (most recent first)
Conditional
Answers a yes/no question based on a setAll
-
customers.All(c => c.Active == true)
Is every customer in the set active?
-
customers.Any(c => c.LastOrderDate > DateTime.Now.AddDays(-7))
Did any customer in the set make an order in the past 7 days?
Projection
Transforms a set of objects in one form to a set of objects in another formSelect
-
IEnumerable<string> names = customers.Select(c => c.FirstName)
Select the first name of every customer (transform each Customer object to a string object)
Extension methods always return a COPY:
- IEnumerable<Customer> sorted = customers.OrderBy(c => c.LastName)