LINQ: Writing Query Expressions in C# .NET
What does LINQ mean? .NET Language Integrated Query
Using LINQ to query C# / VB.NET objects
LINQ is one of .NET's newest features for Framework 3.0.This Integrated Query language is extremely useful for simplifying queries directly in code. For example, you may be familiar with T-SQL expressions. One of LINQ's great capabilities that I will cover in this blog is the ability to query data in much the same fashion, including Counts, Aggregates, Joins, and much more. I have also written other blogs about LINQ-to-SQL / LINQ-to-Stored Procedures as a way of generating business objects. And I have also discussed LINQ as an entity framework vs NHibernate to discuss that possibility. There is also LINQ-to-XML, all of which provide a single syntax for querying many things. But, this article discusses its core functionality for LINQ-to-Objects. To start us off, I have taken a descriptive excerpt from Microsoft's MSDN site about LINQ:
After two decades, the industry has reached a stable point in the evolution of object-oriented (OO) programming technologies. Programmers now take for granted features like classes, objects, and methods. In looking at the current and next generation of technologies, it has become apparent that the next big challenge in programming technology is to reduce the complexity of accessing and integrating information that is not natively defined using OO technology. The two most common sources of non-OO information are relational databases and XML.
Rather than add relational or XML-specific features to our programming languages and runtime, with the LINQ project we have taken a more general approach and are adding general-purpose query facilities to the .NET Framework that apply to all sources of information, not just relational or XML data. This facility is called .NET Language-Integrated Query (LINQ).
We use the term language-integrated query to indicate that query is an integrated feature of the developer's primary programming languages (for example, Visual C#, Visual Basic). Language-integrated query allows query expressions to benefit from the rich metadata, compile-time syntax checking, static typing and IntelliSense that was previously available only to imperative code. Language-integrated query also allows a single general purpose declarative query facility to be applied to all in-memory information, not just information from external sources.
.NET Language-Integrated Query defines a set of general purpose standard query operators that allow traversal, filter, and projection operations to be expressed in a direct yet declarative way in any .NET-based programming language. The standard query operators allow queries to be applied to any IEnumerable<T>-based information source. LINQ allows third parties to augment the set of standard query operators with new domain-specific operators that are appropriate for the target domain or technology. More importantly, third parties are also free to replace the standard query operators with their own implementations that provide additional services such as remote evaluation, query translation, optimization, and so on. By adhering to the conventions of the LINQ pattern, such implementations enjoy the same language integration and tool support as the standard query operators.
The extensibility of the query architecture is used in the LINQ project itself to provide implementations that work over both XML and SQL data. The query operators over XML (LINQ to XML) use an efficient, easy-to-use, in-memory XML facility to provide XPath/XQuery functionality in the host programming language. The query operators over relational data (LINQ to SQL) build on the integration of SQL-based schema definitions into the common language runtime (CLR) type system. This integration provides strong typing over relational data while retaining the expressive power of the relational model and the performance of query evaluation directly in the underlying store.
LINQ Lambda Expressions
You may be familiar with inline delegates, or function predicates for passing functionality into another function.
Func<int, int, int> function = (a,b) => a + b;
This statement consists of three sections.
-
A declaration: Func<int, int, int> function
-
An equals operator: =
-
A lambda expression: (a,b) => a + b;
This is the equivalent of writing this delegate:
public int function(int a, int b)
{
return a + b;
}
101 LINQ Samples and Expressions
The following is a great resource from Microsoft's MSDN about querying:
Restriction Operators
Projection Operators
Partitioning Operators
Ordering Operators
|
Grouping Operators
Set Operators
Conversion Operators
Element Operators
Generation Operators
Quantifiers
|
Aggregate Operators
Miscellaneous Operators
Custom Sequence Operators
Query Execution
|
LINQ Developer Wiki
Here is also a great wiki that discusses all the functionalities of LINQ including all individual capabilities of LINQ-to-SQL, LINQ-To-Objects, and LINQ-to-XML located here.
If you have any other questions, Contact me!
Thursday, September 25, 2008 9:29:26 AM