LINQKit: “C# 3.0 In A Nutshell” Extras

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

  • PredicateBuilder

    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:

    var predicate = PredicateBuilder.False<Product>();
    <span class="kwrd">foreach</span> (<span class="kwrd">string</span> keyword <span class="kwrd">in</span> keywords)
        <span class="kwrd">string</span> temp = keyword;
        predicate = predicate.Or(p => p.Description.Contains(temp));

    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.

  • LINQBridge

    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.

    <span class="kwrd">int</span>[] numbers = { 5, 15, 7, 12 };
    var query = numbers
      .Where (n => n > 10)
      .OrderBy (n => n)
      .Select (n => n * 10);

Check out the extras page for more code samples and the library download with demo solution and library source code.

Technorati Tags: C#,LINQ,LINQPad,C# 3.0
Share With Friends:

How did I learn that?

As a bonus for coming here, I'm giving away a free newsletter for web developers that you can sign up for from here.

It's not an anything-and-everything link list. It's thoughtfully collected picks of articles and tools, that focus on Angular 2+, ASP.NET (4.x/MVC5 and Core), and other fullstack developer goodies.

Take it for a test ride, and you may unsubscribe any time.

You might also want to support me by checking these out [Thanks]: