background preloader

Linq

Facebook Twitter

N-tier

Mitsu's blog : Linq: how to share parameters between lambda. Before going into Linq, here is again one of my pictures: Le Louvre by night, Paris When using Linq to objects, you will quickly feel the need to pass some parameters from a method to another but it’s not so easy because each Linq method is not calling the following one. In a Linq sequence, each method is using the result computed by the previous one. So, local contexts are not visible from one method to another. The compiler is using two technical different ways to let parameters go out of a method. As an example, let’s first see how the .SelectMany() method is working. var values1 = new string[] { "1", "2" }; var values2 = new string[] { "A", "B", "C" }; var q = from s1 in values1 from s2 in values2 select s1 + s2; This very little example shows that s1 and s2 are both accessible in the select.

Var values1 = new string[] { "1", "2" }; var values2 = new string[] { "A", "B", "C" }; var q = values1.SelectMany(s1 => values2.Select(s2 => s1 + s2)); Let’s focus on the SelectMany parameter: Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library) - Sc. LINQ (language integrated query) is one of the new features provided with VS 2008 and .NET 3.5. LINQ makes the concept of querying data a first class programming concept in .NET, and enables you to efficiently express queries in your programming language of choice.

One of the benefits of LINQ is that it enables you to write type-safe queries in VB and C#. This means you get compile-time checking of your LINQ queries, and full intellisense and refactoring support over your code: While writing type-safe queries is great for most scenarios, there are cases where you want the flexibility to dynamically construct queries on the fly.

For example: you might want to provide business intelligence UI within your application that allows an end-user business analyst to use drop-downs to build and express their own custom queries/views on top of data. Traditionally these types of dynamic query scenarios are often handled by concatenating strings together to construct dynamic SQL queries. Scott. Is there a pattern using Linq to dynamically create a filter? - C# 3.0 in a Nutshell - PredicateBuilder. Dynamically Composing Expression Predicates Suppose you want to write a LINQ to SQL or Entity Framework query that implements a keyword-style search. In other words, a query that returns rows whose description contains some or all of a given set of keywords. We can proceed as follows: IQueryable<Product> SearchProducts (params string[] keywords) { IQueryable<Product> query = dataContext.Products; foreach (string keyword in keywords) { string temp = keyword; query = query.Where (p => p.Description.Contains (temp)); } return query; } The temporary variable in the loop is required to avoid the outer variable trap, where the same variable is captured for each iteration of the foreach loop.

So far, so good. Of all the things that will drive you to manually constructing expression trees, the need for dynamic predicates is the most common in a typical business application. Using PredicateBuilder Here's how to solve the preceding example with PredicateBuilder: PredicateBuilder Source Code How it Works. Solid Code: Left Outer Join in LINQ.