Tech Brief

All About LINQ

Integrating data queries into VB and C#

Language Integrated Query (LINQ) refers to a collection of features in the Microsoft .NET 3.5 Framework that enables developers to embed queries against in-memory or external data directly into their programs. LINQ can close the gap between programming objects and data by providing a common query facility over different types of data, built into the .NET Framework.

How Does LINQ Work?
LINQ integrates query functionality into a programming language. Here are the components of LINQ:

Lambda Expressions
Lambda expressions are inline functions that operate on an input value. For example, the lambda expression "s=>s*1.10" can be read "given s, return s multiplied by 1.10." Similarly, the lambda expression "employee=>employee.Salary>50000" can be read "given employee, return whether or not employee.salary is greater than $50,000." Like delegates, lambda expressions can be passed to other methods. Unlike delegates, however, lambda expressions may be passed as expression trees (as opposed to executable IL) to other methods, giving the called method the ability to reason about the expression itself.

Query Methods
Queries are built using a set of Query Methods that are understood by the system; for example, "Select," "Where" and "OrderBy." Most of the query methods take and return strongly typed iterators (IEnumerable). A number of the query methods additionally take a delegate or lambda expression and apply it to the members of the input iterator. For example, the Where method takes a function that returns a boolean value (such as our lambda expression "employee=>employee.Salary>50000") and applies it to the input iterator, returning a new iterator containing only those values for which the function evaluated to true. Similarly, the Select method shapes an input iterator by taking a function which may return a different type (such as "employee=>employee.Salary*1.10") and applying it to the input iterator. By passing the output of the Where method as the input to the Select method, we end up with an iterator that returns the salary, increased by 10 percent, of all employees with a current salary greater than $50,000.

Examples of LINQ Through Language Comprehensions in C# and VB
[click image for larger view]
Examples of LINQ Through Language Comprehensions in C# and VB.

Extension Methods
Extension methods enable external classes to define methods that appear on instances of existing compiled classes. For example, you could define an extension method "Get ExtendedPrice" that returned the Unitprice * Quantity on an existing class OrderItem. You could then call this method on OrderItem just like any other method. Extension methods are implemented as static methods that take an instance of an existing class as the first argument, but appear as instance methods on the existing class.

There is a public static class, System.Linq.Enumerable, that implements the standard query methods as extension methods on IEnumerable. Thus, by including the System.Linq namespace, query methods appear on any collection, array or sequence of values that implements IEnumerable.

Query Comprehensions
Queries can be built programmatically from any language by calling the query methods with the appropriate lambda expression. Query Comprehensions add keywords to C# and VB for common query operators like Select, Where, GroupBy and OrderBy, enabling common queries to be written without explicitly calling methods or specifying lambda expressions.

Anonymous Types & Object Initialization Expressions
The result of the Select operator is an IEnumerable, but it may not be convenient to define a type for every possible projection. Anonymous types can be defined inline, based on members from other structured values, without supplying names for the type or the members.

Functional Programming
LINQ expressions provide a functional definition of the desired data, rather than prescribing the procedure for yielding a set of results. The entire composed expression is not evaluated until the results are enumerated or explicitly converted to a list. The LINQ implementation chooses the best strategy for coming up with the requested results. So an implementation of LINQ over a relational store doesn't retrieve an entire table and loop through the results applying each expression individually. Instead, the entire LINQ expression gets converted into a SQL query, executed at the store via the relational engine, and the matching results are retrieved locally, with the correct projection, ordering and grouping applied.

LINQ Implementations
In addition to the default implementation of query operators over an IEnumerable, the .NET 3.5 Framework will support querying XML, relational data in SQL Server or exposed through a conceptual Entity model over extended ADO.NET data providers, and data in an ADO.NET Dataset.
comments powered by Disqus

Featured

  • Cloud-Focused .NET Aspire 9.1 Released

    Along with .NET 10 Preview 1, Microsoft released.NET Aspire 9.1, the latest update to its opinionated, cloud-ready stack for building resilient, observable, and configurable cloud-native applications with .NET.

  • Microsoft Ships First .NET 10 Preview

    Microsoft shipped .NET 10 Preview 1, introducing a raft of improvements and fixes across performance, libraries, and the developer experience.

  • C# Dev Kit Previews .NET Aspire Orchestration

    Microsoft's dev team has been busy updating the C# Dev Kit, a Visual Studio Code extension that enhances the C# development experience by providing tools for managing, debugging, and editing C# projects.

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

Subscribe on YouTube

Upcoming Training Events