LINQ and Lambdas

LINQ lets you filter, sort, group and total collections in a few readable lines. You will write lambda expressions and use the LINQ methods you will reach for every day.

Updated

Working with Data in Motion

  1. Data in Transit vs Data at Rest (Apprentice): Stored data is at rest, and data moving between places, such as over a network or from a query, is in transit.
  2. Extract Transform Load (Apprentice): ETL (Extract, Transform, Load) takes data from a source, reshapes it in a series of small steps, and stores it at a destination.

LINQ

  1. Lambda Expressions (Apprentice): A lambda expression is a small method with no name, written where it's used, such as name => name.Length > 4. Pass one to a method like FindAll, RemoveAll or Sort, and that method runs it on each item.
  2. What is LINQ? (Journeyman): LINQ is ETL for the data in your program. Start from any collection, chain methods such as Where, OrderBy and Select that each transform the items, and finish with ToList, Count or First. A query runs when its results are used.
  3. Common LINQ Methods (Journeyman): The LINQ methods you'll use every day. Where, OrderBy, ThenBy and Select filter, sort and reshape; Count, Sum, Average, Min, Max, Any, All and GroupBy summarize; First, FirstOrDefault, Take, Skip and Distinct pick items.
  4. Query Syntax vs Method Syntax (Journeyman): LINQ can also be written in query syntax, from ... where ... orderby ... select, which reads like SQL. The compiler turns it into the same method calls. Method syntax is more common, and some methods, such as Count and First, have no query keyword.

Exercise: SnackShack

  1. Exercise: SnackShack (Journeyman): Answer a snack bar owner's questions with LINQ. What's cheap, what's in each category, what sells best, what never sells, how much came in each day, and one page of the menu at a time, each in one chain of methods.