background preloader

Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library)

Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library)
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#. 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. Traditionally these types of dynamic query scenarios are often handled by concatenating strings together to construct dynamic SQL queries. Downloading the LINQ Dynamic Query Library Included on the VS 2008 Samples download page are pointers to VB and C# sample packages that include a cool dynamic query LINQ helper library. Simple Dynamic Query Library Example Using the LINQ DynamicQuery library I could re-write the above query expression instead like so: Hope this helps, Scott Related:  ASP.Net & MVC

Dynamic LINQ Queries with Expression Trees It's possible to build up dynamic LINQ queries or queries with several conditional criteria. In fact there are several options for doing this, including the use of expression trees Like it or not, LINQ is here to stay. Personally I think this is a good thing. LINQ brings a lot of benefits: No magic strings (makes refactoring tools much more effective) SQL-like syntax outside of the database LINQ providers for various data sources (SQL, Entities, Objects, nHibernate, XML, etc) Initially, I thought that there was a big limitation in not being able to build up dynamic queries or queries with conditional criteria. In this article, we will review various approaches for building dynamic LINQ queries, starting with some fairly straightforward options and moving on to building the query from scratch with Expression Trees. Hopefully you'll enjoy the ride. A Little Background For purposes of this discussion, we will use the latter syntax and call it Method chaining. Type Safe Dynamic Sorting

Mitsu&#039;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. 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: SelectMany(Func<TSource, IEnumerable<TResult>> selector). The method must return an IEnumerable<TResult>. Let’s see how the “let” keyword works.

ASP.NET MVC: Do You Know Where Your TempData Is? « Greg Shackles I recently discovered that despite the fact that I’d been using the TempData dictionary in my applications, I didn’t really have a full grasp on what it was doing behind the scenes. Of course this meant learning the lesson the hard way once something stopped working like I thought it would. I only really had myself to blame since in the end it was a simple case of RTFM, but it seemed like a good opportunity to dig in and see how TempData really works. What Is TempData? Let’s start by describing what TempData gives you. Where Is TempData Stored? This is the part that came back to bite me. You might have noticed that I said that TempData is kept in the session by DEFAULT, meaning you are not tied to that if you decide you don’t like it that way. ITempDataProvider and SessionStateTempDataProvider In the System.Web.Mvc namespace you’ll find a very simple interface called ITempDataProvider that looks like this: Not much to see there. How the TempData Provider Gets Used A MongoDB TempData Provider

Is there a pattern using Linq to dynamically create a filter? - Injecting Open Graph Protocol Content Into your ASP.NET MVC Site : On All Things Web In many social networking sites, pasting a URL from a news site into Facebook or Linked In displays a nice synopsis of that link. The URL pasted into such status box is read, capturing information about the page and it’s specific contents, such as a title,description, thumbnail, etc. One common technique used is to extract the Open Graph Protocol ( markup defined in the header, which is providing the additional metadata. An open graph tag is a meta tag using property names prefixed with “og:”, each page can have its own specific metadata that describes the purpose of that page. I had already created a site without open graph tags,so I began to think of a way to incorporate them with little effort. @{ ViewBag.Title = “Create a New Group”; ViewBag.Description = “Use this feature to create a new group.”; } Notice I omitted the image property, which will then use the site’s logo link.

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

The Open Graph protocol Don’t Do Role-Based Authorization Checks; Do Activity-Based Checks I’ve built a few dozen security mechanisms in my career. Unfortunately, I kept getting it wrong, hence the need to keep building them. Over the years, though, I learned a number of different ways that a security system can be built. One of my favorite ways to build authorization systems is through the use of role-based security. The idea is fairly simple: you assign users to roles and roles have permissions. That way you have a nice abstraction that people can be assigned to, as a role, so that you don’t have to assign the same 5, 10, or 500 permissions to every user in your system. Stop Using Roles For Authorization Checks When it comes time to check authorization rules, we often see the role being checked. For example, look at the MSDN documentation for ASP.NET MVC’s AuthorizeAttribute. The Authorize attribute lets you indicate that authorization is restricted to predefined roles or to individual users. What’s Wrong With Role-Based Authorization Checks? Documentation Or in ruby:

Related: