The Language Integrated Query (LINQ) technology set provides a concise, symmetrical, and strongly typed manner to access a variety of data stores. First a review of the key C# programming constructs that enable LINQ.

LINQ-Specific Programming Constructs


At a high level, LINQ can be understood as a strongly typed query language embedded directly into the grammar of C#.

Implicit Typing of Local Variables


The var keyword of C# allows you to define a local variable without explicitly specifying the underlying data type. The variable is still strongly typed as the compiler will determine the correct data type based on the initial assignment. Many LINQ queries will return a sequence of data types which are not known until compile time.

Object and Collection Initialization Syntax


Object initialization syntax allows you to create a class or structure variable and set any number of its public properties in one fell swoop. This results in a compact and easy on the eyes syntax to prepare objects. C# also allows you to use a similar syntax to initialize collections of objects. This snippet uses collection initialization syntax to fill a List<T> of Rectangle objects:

List<Rectangle> myListOfRects = new List<Rectangle>
{
  new Rectangle {TopLeft = new Point { X = 10, Y = 10},
                 BottomRight = new Point { X = 200, Y = 200}},
  new Rectangle {TopLeft = new Point { X = 2, Y = 2 },
                 BottomRight = new Point { X = 100, Y = 100}},
  new Rectangle {TopLeft = new Point { X = 5, Y = 5 },
                 BottomRight = new Point { X = 90, Y = 75}}
};

When combined with implicit typing of local variables, this also allows you to declare an anonymous type which is useful when creating a LINQ projection.

Lambda Expressions


=> is the C# lambda operator. This operator allows you to build a lambda expression, which can be used any time you invoke a method that requires a strongly typed delegate as an argument. Lambdas greatly simplify how you work with delegates in that they reduce the amount of code you must author by hand. A lambda expression can be broken down into the following usage:

( ArgumentsToProcess ) => { StatementsToProcessThem }

List<int> list = new List<int>();
list.AddRange(new int[] { 20, 1, 4, 8, 9, 44 });
List<int> evenNumbers = list.FindAll(i => (i % 2) == 0);

Article notes

What is the unabbreviated form of LINQ?
What provides a concise, symmetrical, and strongly typed manner to access a variety of data stores in C#?
What can be understood as a strongly typed query language embedded directly into the grammar of C#?
What keyword in C# allows you to define a local variable without explicitly specifying the underlying data type?
Some functionality of the LINQ to Objects API can only be accessed by calling extension methods of what class?
What type defines the various extension methods that the LINQ query operators such as from, in, where, orderby, select are shorthand for?
When LINQ Queries are compiled, the C# compiler translates all C# LINQ operators into calls on methods of what class?
What generic delegate type have "a great many" of the methods of Enumerable been prototyped to take as arguments (LINQ)?
What term refers to the act of applying LINQ queries to arrays and collections?
What term refers to the act of using LINQ to manipulate and query XML documents?
What term refers to an aspect of LINQ that lets you use LINQ queries with the ADO.NET EF Core API?
What term refers to parallel processing of data returned from a LINQ query?
The words from, in, where, orderby, and select are examples of what in C#?
What specific syntax of LINQ is this? IEnumerable<string> subset = from game in currentVideoGames where game.Contains(" ") orderby game select game;
What specific syntax of LINQ is this? IEnumerable<string> subset = currentVideoGames.Where(g => g.Contains(" ")).OrderBy(g => g).Select(g => g);
What will you always want to make use of when capturing the results of a LINQ query in C#?
What is the type of the return value (the generic interface that it implements) of a LINQ query (most of the time)?
What is it called that describes how LINQ query expressions that return a sequence do not actually evaluate until you iterate over the resulting sequence?
What is a thing to note about a LINQ statement that selects a single element (such as by using First()/FirstOrDefault()/Single()/SingleOrDefault()) regarding execution?
What is the difference between First()/FirstOrDefault() and Single()/SingleOrDefault()?
What are the three operators that every LINQ query expression is built from?
What is the general template for a LINQ query expression using the operators from, in, and select?
What is the general template for a LINQ query expression using the three basic operators and also where to obtain a specific subset?
What is the extension method that can be used with the result of a LINQ query to remove duplicate entries?
What is one (or a couple) example of an extension method that can be used with LINQ queries to perform an aggregation operation on the result set?
Previous Next