The "C# 3.0 in a nutshell" book has some neat free extras that are worth mentioning for those who haven’t already heard of (they have been released for long). Those are like must-have LINQ tools and helpers.
The homepage of LINQKit (the major part of the extras I’m going to cover here) provides great information and short code samples about the components:
- LINQPad This is a snippet compiler (application to run/try small codes in separation than big VS projects, like this), that’s customized for LINQ queries. You can use the SQL Server Query Analyzer -like application to try your codes on the tables in the DB, and see the resultant output and SQL query. Very useful for learning LINQ and/for for making up complex LINQ Queries
This is a dynamic query library. If you ever tried LLBLGen (one of the best .NET OR mappers), it’s equivalent to PredicateExpression and PredicateFactory stuff. If not, then it’s a dynamic way of creating queries. Sometimes you want to add/remove conditions to/from your query based on runtime decisions, and you can’t do that with the standard query way, so, this library allows you to write code like this:
Of course you could wrap the ".And" and ".Or" parts in an "if" statement based on any check you like, which is the real power here!
Improvements / Workarounds
When you deal with LINQ much enough, you’ll get many limitations (as everything else :D). For example, sometimes you cannot set conditions on related entities (like setting conditions on the "Pruchase"s of a "Customer". Also, you’ll want to do more with the expressions you use, mainly combine two different expressions together, use advanced queries with related entities, doing more stuff asynchronously, etc. The LINQKit has many small extension methods you use in your normal query or your dynamic query to work around that and more.
If you find that you have to work on some .NET 2.0 project, it doesn’t mean you lose LINQ. LINQBridge allows you to write queries like this one.
Check out the extras page for more code samples and the library download with demo solution and library source code.