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

c# - Dynamic LINQ - Is There A .NET 4 Version Source Code Search Engine 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

Using jqGrid’s search toolbar with multiple filters in ASP.NET MVC Download sample - 350 KB Introduction Very often, we have a requirement to display data in tabular form and manipulate it. In classic ASP.NET, built-in controls are available. It's always better to use custom scripts to solve this problem in ASP.NET MVC. jqGrid is one of such solutions. Background jqGrid jqGrid is a plugin for jQuery, which allows you to display data in tabular form with greater functionality. supports tables within tables supports data editing supports tree-like structure to display data supports themes on the jQuery UI records searching, filtering of each column, sorting by columns, page navigation, etc. License "The jqGrid is released under the GPL and MIT licenses. Official site: Using the code Using jqGrid Consider which files we must include to use this plug-in: <link href="../.. It's obvious that we should include jQuery and JQuery UI scripts, the jqGrid plug-in, and the jqGridHomeIndex.js file which contains the grid declaration. Search jqGrid processing Paging:

Dependency Patterns: Optional Dependencies and Primal Dependenci Jim Newkirk is blogging about the down side of setup and teardown methods in test classes, and why you shouldn’t use them. Setup and teardown methods attract entropy faster than an outsource programmer with his first patterns book. Jim’s new framework, xUnit.NET doesn’t have primitives for setup and teardown, although it sounds like there are mechanisms that could be used to accomplish the same kind of thing. Roy feels that xUnit.NET isn’t quite there yet. I think that Jim is on the right track, but I’m the kind of guy that feels that a test class’s greatest responsibility is to document behavior in the clearest possible way, even if that means sacrificing design qualities best reserved for functional code – like reuse. Unit test patterns in .NET can learn a lot from RSpec, where setup and teardown methods, as well as test methods themselves, are kept small – tiny, actually – by breaking up multiple test classes into smaller, more focused modules.

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# 4.0/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