# LINQ - Language INtegreated Query
LINQ – Language INtegrated Query – is a powerful feature of the C# language that allows SQL-like querying of collections of data, including data that is stored in databases. LINQ statements can be written in two ways: using Query syntax, and using Method syntax.These ways are not in conflict either; you can actually combine Query syntax and Method syntax in a single statement.
# Query Syntax
Query syntax appears, at first glance, to be a lot like SQL. LINQ queries use keywords that closely resemble keywords in SQL, but LINQ's keywords act as operators in that they are used to form an expression. LINQ query operators perform specific processing acts on the objects that act as their "operands". They flow together in a kind of "sequence" and culminate with the select
.
Query syntax includes seven major clauses (five of which are the more commonly used).
from
clause
~ A query expression must begin with a from clause. Additionally, a query expression can contain sub-queries, which also begin with a from clause. (see from clause)
where
clause
~ The where clause is used in a query expression to specify which elements from the data source will be returned in the query expression. (see where clause)
select
clause
~ In a query expression, the select clause specifies the type of values that will be produced when the query is executed. The result is based on the evaluation of all the previous clauses and on any expressions in the select clause itself. A query expression must terminate with either a select clause or a group clause. (see select clause)
group
clause
~ The group clause returns a sequence of IGrouping<TKey, TElement> objects that contain zero or more items that match the key value for the group. (see group clause)
orderby
clause
~ In a query expression, the orderby clause causes the returned sequence or subsequence (group) to be sorted in either ascending or descending order. Multiple keys can be specified in order to perform one or more secondary sort operations. (see orderby clause)
join
clause
~ The join clause is useful for associating elements from different source sequences that have no direct relationship in the object model. (see join clause)
let
clause
~ In a query expression, it is sometimes useful to store the result of a sub-expression in order to use it in subsequent clauses. You can do this with the let keyword (see let clause).
An example of the flow seen in a typical LINQ query can be illustrated the following [over-simplified] grammar. (Hover over the blue elliptical items to get a longer description of that part.)
fromtype-nameidentifier in enumerable-expressionorderby expression , ..nascending descendingwhere boolean-expressiongroup group-object by group-obj-propertyselect expressioninto group-identifier
# LinqPad - A Scratch-pad for LINQ
LinqPad is a stand-alone application that you can use in place of creating ad-hoc console applications in order to explore LINQ. While it's not limited to LINQ, it does work very well to explore LINQ-based queries against a database.
The LinqPad editor window allows you to select from three main contexts:
- Expressions - In this simplest context, you simply have to generate an expression in the editor and run the expression. LinqPad will show the output in the Results window (just below the editor).
- Statements - Imagine lines of code in a method, and you have the Statements context. Here you write complete program statements – declaring variables, making calculations, performing queries, writing
if
or other flow-control statements, etc. – and LinqPad executes these like it was the body of some method call. If you want to output anything to the results window, simply call the.Dump()
extension method that LinqPad includes for all objects. - Program - This context most closely represents a conole application. You are given a
Main()
method and you can add additional methods and/or classes to build a complete console-like application. In this context, however, instead of callingConsole.WriteLine(...)
, you continue to use the.Dump()
method to output content to the results window.
LinqPad allows you to connect to a database. Clicking "Add Connection..." opens the "Choose Data Context" dialog. Click "Next" (leaving the default) move you to the "LINQPad Connection" dialog where you specify your server name and your existing database. From there, you can see the database and its tables and columns in the explorer pane on the left.
- Click "Add Connection..."
- Accept the default LINQ to SQL driver in the dialog and click "Next".
- In the "Identify your server and database" section, type in the name of your server (. for the local machine) and select your existing database in the dropdown for "Specify new or existing database".
You can use the tree-view in the Explorer Panel to expand the tables and view column names and data types as well as navigation properties to other tables.