Questions tagged [dynamic-linq]

Dynamic LINQ is a small library on top of the .NET framework 3.5 or later which allows to build string based query expressions. A common use case is to create LINQ queries at runtime in contrast to constructing queries at compile time.

Dynamic LINQ is a small library on top of the .NET framework 3.5 or later which allows to build string based query expressions.

Dynamic LINQ is not part of the .NET framework but is provided as a sample to Visual Studio 2008. The single C# file consists of the namespace System.Linq.Dynamic which includes a set of extension methods for IQueryable<T> - for instance overloads of Where, Select, OrderBy, GroupBy which expect a string as parameter instead of a lambda expression to define a query.

The query string is parsed and transformed into an expression tree by using Reflection. The Dynamic LINQ extension methods throw a ParseException if the syntax of the query string is invalid or the string contains unknown operators or properties.

Dynamic LINQ can be used with any LINQ data provider like LINQ to Objects, LINQ to Entities, LINQ to SQL or LINQ to XML.

Example in C#

The strongly-typed LINQ query ...

var query = Northwind.Products
                     .Where(p => p.CategoryID == 2 && p.UnitPrice > 3)
                     .OrderBy(p => p.SupplierID);

... can be expressed in Dynamic LINQ in the following way:

var query = Northwind.Products
                     .Where("CategoryID = 2 And UnitPrice > 3")
                     .OrderBy("SupplierID");

Links

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

573 questions
5
votes
2 answers

Dynamic LINQ to DataTable: why using hardcode string "it" as DataRow

Assume I have a DataTable with three columns C1, C2, C3: var dt = new DataTable(); dt.Columns.Add("C1", typeof (int)); dt.Columns.Add("C2", typeof (string)); dt.Columns.Add("C3", typeof(string)); dt.Rows.Add(1, "1", "1"); dt.Rows.Add(1, "1",…
cuongle
  • 74,024
  • 28
  • 151
  • 206
5
votes
1 answer

DbSet.Include() causes SELECT N+1 when used in extension method

I have an extension on IQueryable that allows passing in delimited string of property names which, when used causes query not to construct JOINs and effectively causes SELECT N+1 issue. What I noticed is that if I call native EF extension…
zam6ak
  • 7,229
  • 11
  • 46
  • 84
5
votes
2 answers

C# Dynamic LINQ: Select with Sum on dictionary indexer

I'm using dynamic LINQ to create a groupby and select on the fly. My items are key/value collections (dictionaries) so they contain no properties (it's a design requirement and can't be changed). I was able to solve the groupby part in another…
sean.net
  • 735
  • 8
  • 25
4
votes
2 answers

Dynamic LINQ: Specifying class name in new clause

With Dynamic LINQ, what changes need to be done to have fields of the given class? For example, how can the following C# query be reproduced in DLinq: var carsPartial = cars.Select(c => new {c.year, c.name, make = new maker() {name = c.make.name}…
Seph
  • 8,472
  • 10
  • 63
  • 94
4
votes
1 answer

LINQ Dynamic Query Library setup

I am having trouble setting up this Dynamic Linq Library so i can use Dynamic where clauses. Can someone advise me onhow to add this library to my project and reference correctly.…
kds6253
  • 835
  • 1
  • 12
  • 17
4
votes
1 answer

Lambda Expression "Contains"

I use a dynamic filter, that filters a collection using object properties, operators and values. Now, if the property is a string, the operator is "contains" and the value is "word", the filtered objects containing the "world" should be filtered…
serhio
  • 28,010
  • 62
  • 221
  • 374
4
votes
1 answer

VB.NET and the System.Linq.Dynamic namespace

I would use the System.Linq.Dynamic. I added the specified Dynamic.vb file, that starts like this : Option Strict On Option Explicit On Imports System.Collections.Generic Imports System.Text Imports System.Linq Imports…
serhio
  • 28,010
  • 62
  • 221
  • 374
4
votes
2 answers

Building a common set of methods that can operate on any linq table

Problem: We make extensive use of a repository pattern to facilitate read/write operations on our datastore (MS SQL using LINQ) across multiple applications and subsections of functionality. We have series of methods that all do something similar to…
amber
  • 1,067
  • 3
  • 22
  • 42
4
votes
2 answers

How to name fields using Dynamic Linq?

I have a huge query that is being made dynamically, but I want the select statement to not output the column names, buut custom values. FOr example, if I am doing a normal Linq query, I can do something like this: var v = from p in db.items select…
naspinski
  • 34,020
  • 36
  • 111
  • 167
4
votes
2 answers

LINQ DynamicLibrary: How to extract count and list from IQueryable

I have started experimenting a bit with the LINQ DynamicLibrary. I am trying to replace a bunch of LINQ statements with a few or just one Dynamic LINQ query. My existing static query is as follows: private List
4
votes
4 answers

Dynamic Linq Where IN

How could I define a where in criteria with Dynamic Linq? I tired workaround below but it must not work, because it doesn't make sense! context.Records.Where("@0.Contains(ID)", new object[]{ new string[] {"1", "2", "3"} }); // throws error No…
Ali Bahrami
  • 5,935
  • 3
  • 34
  • 53
4
votes
0 answers

Union within the GroupBy - System.Linq.Dynamic

I am using Dynamic Linq Library (System.Linq.Dynamic.dll) for dynamic data retrieval, But I am facing some problem in converting the below code to dynamic linq. Consider I have following class A public class A { public int Id { get; set; } …
Harisyam M
  • 407
  • 1
  • 3
  • 14
4
votes
2 answers

C# - Dynamic Linq left outer join on multiple properties

i want to do left outer join in Dynamic Linq, but i can't get the syntax right. In SQL it would look like this: SELECT col1, col2, col3 from tableA as a LEFT OUTER JOIN tableB as b on a.col1 = b.col1 AND a.col2 = b.col2 AND a.col3 = 1 In dynamic…
Oktay Myumyunov
  • 120
  • 1
  • 16
4
votes
2 answers

How to filter child collection with linq dynamic

I'm trying to filter results for user request. For instance you have orders and order details and products is child collection. When user wants to filter by product I'm getting an error because of No property or field 'PRODUCTS' exists in type…
Kadir
  • 3,094
  • 4
  • 37
  • 57
4
votes
1 answer

How to do Linq on ITypedList?

So far, I find Linq can be used on existing fields and properties of a class, not on virtual properties. In other words, ITypedList can not work with Linq, even by dynamic Linq. I tried the following code: IQueryable contact ; ... dynamic l…
Ying
  • 526
  • 4
  • 16