background preloader

C# 4.0/3.0 in a Nutshell - PredicateBuilder

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 Related:  LINQ

Json.NET 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: trirand.com. 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:

LINQ provider basics Learn how to create custom LINQ providers. Introduction LINQ (Language Integrated Query) works as a middle tier between data store and the language environment. From a developer's point of view, it is just a new pattern for querying data from multiple data structures directly in the IDE. Behind the scenes it does a whole lot of tasks like expression processing, validation and calling the right routine to fetch data or build a query to run in SQL Server. In short, LINQ stands as common query gateway between the language and the data store. Figure 1: LINQ workflow (from language to data store) The purpose of this article is to teach you how to extend this query pattern in order to create your own custom provider. Creating and Executing a Query LINQ is a new level of language abstraction that supports querying virtually any data source - such as SQL, XML and in-memory data structures like Dictionary, List and more. Note that a class must implement IQueryable to be queried by a user. 3. 4. 5.

Is there a pattern using Linq to dynamically create a filter? - Dynamic Querying with LINQ-to-Entities and Expressions Introduction I’ve recently been working on a service-oriented WCF application that encapsulates all its data-access into a service layer. What I’ll show in this article is not limited to SOA or WCF applications and could be used just as well in OO or component-oriented projects that are designed to have all data-access in a layer whose responsibilities are limited to getting and putting data in a database. Technologically, I’m using LINQ-to-Entities, which has a significant impact on the code that follows, so if you are using something else (other than perhaps LINQ-to-SQL), this article may not apply. The Problem The interesting problem to solve in a data-access layer is retrieving a filtered, sorted and paged recordset for presentation in a UI grid. Setup I’ll try to reduce this just to the essentials, so forget that I’m really using WCF. Let’s also assume that you don’t want to expose my EDMX entities beyond the data-access layer. Hide Copy Code Adding Filtering, Sorting and Paging

How Duck Typing Benefits C# Developers David Meyer recently published a .NET class library that enables duck typing (also sometimes incorrectly described as Latent Typing as Ian Griffiths explains in his campaign to disabuse that notion) for .NET languages. The term duck typing is popularly explained by the phrase If it walks like a duck and quacks like a duck, it must be a duck. For most dynamic languages, this phrase is slightly inaccurate in describing duck typing. To understand why, let’s take a quick look at what duck typing is about. Duck Typing Explained Duck typing allows an object to be passed in to a method that expects a certain type even if it doesn’t inherit from that type. I emphasize that last phrase for a reason. In many (if not most) dynamic languages, my object does not have to support all methods and properties of duck to be passed into a method that expects a duck. The Static Typed Backlash Give me static types or give me death! Well, actually I do understand...kinda. C# has used duck typing for a long time

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#. 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. 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 Scott

10 Tips to Improve your LINQ to SQL Application Performance | Si Hey there, back again. In my first post about LINQ I tried to provide a brief(okay, bit detailed) introduction for those who want to get involved with LINQ to SQL. In that post I promised to write about a basic integration of WCF and LINQ to SQL working together, but this is not that post. Since LINQ to SQL is a code generator and an ORM and it offers a lot of things, it is normal to be suspicious about performance of it. Hence I summed up 10 important points for me that needs to be considered during tuning your LINQ to SQL’s data retrieval and data modifying process: 1 – Turn off ObjectTrackingEnabled Property of Data Context If Not Necessary If you are trying only to retrieve data as read only, and not modifying anything, you don’t need object tracking. using (NorthwindDataContext context = new NorthwindDataContext()) { context.ObjectTrackingEnabled = false; } 2 – Do NOT Dump All Your DB Objects into One Single DataContext 3 – Use CompiledQuery Wherever Needed I can hear you saying “What?

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. So, local contexts are not visible from one method to another. 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).

Using LINQ Queries Table of contents Introduction Language INtegrated Queries are SQL-like C# queries that can be used to manipulate collections of objects. In this article, I will show some cases of usage that show how LINQ can be used to query collections of objects. The goal of this article is to be a beginners guide for LINQ, and a reference/reminder for others. Background When people hear about LINQ, they in most cases think about something like the Entity Framework, i.e., the possibility to write queries directly in C# code that will be directly translated to SQL statements and executed against the database. There are a lot of extensions of LINQ that translate queries to SQL, XML/XPath, REST, etc. There are two forms of LINQ operations - queries and functions. Queries In the LINQ package is added a set of predefined operators (queries) that enable developers to create SQL-like queries on a collection of objects. Hide Copy Code from <<element>> in <<collection>> where <<expression>> select <<expression>>

C# in Depth: The Beauty of Closures Note: this article has been translated into Serbo-Croatian by Anja Skrba from webhostinggeeks.com. Some time soon, I want to write about the various Java 7 closures proposals on my blog. However, when I started writing that post I found it was difficult to start off without an introduction to closures. As time went by, the introduction became so long that I feared I'd lose most of my readers before I got to the Java 7 bit. As chapter 5 in the book is largely about anonymous methods and how they provide closures to C#, it seemed appropriate to write this article here. Most articles about closures are written in terms of functional languages, as they tend to support them best. What are closures? To put it very simply, closures allow you to encapsulate some behaviour, pass it around like any other object, and still have access to the context in which they were first declared. It's easiest to look at a lot of the benefits (and implementations) of closures with an example. What's the output?

c# - Dynamic LINQ - Is There A .NET 4 Version LINQ Expression Builder - Home

Related:  .Net.Net