background preloader

LINQ

Facebook Twitter

C# - Linq UNION query to select two elements. How to filter a DataTable dynamically using LINQ? Many ways to query a DataTable. 1.)

How to filter a DataTable dynamically using LINQ?

You can use the DataTable.Select method. However, the results are an array of DataRow which is not bindable.2.) You can use a DataView which is efficient for sorting, filtering. All DataTables have a default DataView -- DataTable.DefaultView3. . ) #2 and #3 are the preferred ways for binding the results. //DataViewDataView dv = new DataView(tableOne, "OrderID <> 'XXXX'", "OrderTime desc", DataViewRowState.CurrentRows); Or transform the DataView back to a DataTableDataTable newTable = new DataView(tableOne, "OrderID <> 'XXXX'", "OrderTime desc", DataViewRowState.CurrentRows).ToTable(); Or you can use LINQIEnumerable<DataRow> ordered = tableOne.AsEnumerable() .Where(i => i.Field<String>("OrderID ") !

Queryable.Join(TOuter, TInner, TKey, TResult), méthode (IQueryable(TOuter), IEnumerable(TInner), Expression(Func(TOuter, TKey)), Expression(Func(TInner, TKey)), Expression(Func(TOuter, TInner, TResult))) (System.Linq) C# - How does PredicateBuilder work. C# 5.0/4.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.

C# 5.0/4.0 in a Nutshell - PredicateBuilder

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

Dynamic Querying with LINQ-to-Entities and Expressions

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.

Using LINQ Queries. Table of contents Introduction Language INtegrated Queries are SQL-like C# queries that can be used to manipulate collections of objects.

Using LINQ Queries

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>> Introducing LINQ—Language Integrated Query. Download source code - 17.7 KB Introduction In my previous three articles on CodeProject.com, I have explained the fundamentals of Windows Communication Foundation (WCF), including: If you have followed those three articles closely, you should be able to work with WCF now.

Introducing LINQ—Language Integrated Query

Within the last two articles, I have explained how to utilize LINQ and the Entity Framework with WCF, so by now you should also be able to work with LINQ and EF. However, you may not fully understand LINQ and EF by just reading those two articles. In addition to LINQ and EF, some people may still be using LINQ to SQL, which is the first ORM product from Microsoft, or a by-product of the C# team, or a simplified version of EF, or whatever you think and say it is.

Having said so, in the future, I will write the following five articles to explain LINQ, LINQ to SQL, and EF: In this article, I will cover the following topics: What is LINQ Let us see an example first. Hide Copy Code But what do from, where, and select mean here?