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
10
votes
2 answers

System.Linq.Dynamic.Core vs System.Linq.Dynamic

What is the difference between System.Linq.Dynamic.Core and System.Linq.Dynamic? I am currently using System.Linq.Dynamic and it does not contain support for Select and SelectMany (among other extension methods). Does System.Linq.Dynamic.Core…
jjf1978
  • 199
  • 1
  • 2
  • 13
10
votes
3 answers

Dynamic LINQ GroupBy Multiple Columns

I need to translate the following LINQ query to Dynamic LINQ that accepts several grouping columns based on user input. Basically I have a bunch of dropdownlists that apply groupings and I don't want to enumerate every combination of groupings. If…
Daniel Coffman
  • 1,997
  • 3
  • 26
  • 34
9
votes
5 answers

How to use Enums with Dynamic Linq?

I would like to use enumerations in my dynamic LINQ queries. Is it possible, and if, how? Consider the code bellow: using System; using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic; namespace ConsoleApplication2 { …
serhio
  • 28,010
  • 62
  • 221
  • 374
9
votes
3 answers

Building Dynamic LINQ Queries based on Combobox Value

I have a combo box in Silverlight. It has a collection of values built out of the properties of one of my LINQ-to-SQL objects (ie Name, Address, Age, etc...). I would like to filter my results based off the value selected in a combo box. Example:…
Steve G.
  • 590
  • 5
  • 15
9
votes
3 answers

Dynamic Or Clause Linq

Today we currently have a statement like this: var Query = (from dp in db.Patients select dp); var UserID = User.Identity.GetUserId(); if (User.IsInRole("Administrator")) { Query = Query.Where(x => x.AdministratorID ==…
Rob Carroll
  • 377
  • 1
  • 7
  • 16
9
votes
4 answers

Dynamic where clause in LINQ - with column names available at runtime

Disclaimer: I've solved the problem using Expressions from System.Linq.Expressions, but I'm still looking for a better/easier way. Consider the following situation : var query = from c in db.Customers where…
sandesh247
  • 1,658
  • 1
  • 18
  • 24
8
votes
8 answers

Call function in dynamic linq

I'm trying to call a function in a dynamic linq select statement, but im getting error: No property or field 'A' exists in type 'Tuple2' Example code: void Main() { var a = new Tuple(1,1); var b = new[]{ a }; var q =…
Magnus
  • 45,362
  • 8
  • 80
  • 118
8
votes
8 answers

Dynamic LINQ DateTime Comparison String Building - Linq To Entities

I'm using the dynamic LINQ library by Scott Guthrie together with Entity Framework and C#. I have to build my where string into a variable based on several factors and then pass the string variable to the where clause. For some reason, this will…
Ricketts
  • 5,025
  • 4
  • 35
  • 48
7
votes
3 answers

Dynamic Linq - Perform a query on an object with members of type "dynamic"

I am trying to use a dynamic linq query to retrieve an IEnumerable from an object collection (Linq to Object), each of the objects in the collection have an internal collection with another set of objects where the data is stored, these values…
Darsegura
  • 145
  • 2
  • 6
7
votes
2 answers

Logic Evaluator in c# (Evaluate Logical (&& ,|| ) expressions)

In my project there is a Logic evaluation section, it take input as a string which contains logical expressions (true/false) . I want to evaluate this string and return a final Boolean value. string Logic="1&0|1&(0&1)" //string Logic="true AND…
Sreekumar P
  • 5,900
  • 11
  • 57
  • 82
7
votes
2 answers

LINQ Dynamic Query Library

I am building an ASP.Net MVC 3 application with Entity Framework 4. When the two pieces of code below are executed, both variables (query1 and query2) have a return type of System.Data.Objects.ObjectQuery Query1 uses a direct…
tcode
  • 5,055
  • 19
  • 65
  • 124
7
votes
4 answers

Dynamic LINQ query to get Field value from Database

is it possible? Public String Get_Filed_By_Id(string table_Name,String Field_Name,string PK_val) { string strRes=""; using(mydbcontext db=new mydbcontext()) { var x=db.table_Name.Where(p=>p.Id=PK_val).FirstOrDefault().Field_Name; …
A_Sk
  • 4,532
  • 3
  • 27
  • 51
7
votes
4 answers

Is the Specification Pattern obsolete when you can use Dynamic LINQ?

Wikipedia states that the Specification Pattern is where business logic can be recombined by chaining the business logic together using boolean logic. With respect to selecting filtering objects from lists or collections it seems to me that Dynamic…
David Robbins
  • 9,996
  • 7
  • 51
  • 82
6
votes
3 answers

Convert my value type to nullable equivalent

I have an ad-hoc reporting system; I have no compile-time knowledge of the source type of queries or of the required fields. I could write expression trees at runtime using the System.Linq.Expressions.Expression factory methods, and invoke LINQ…
Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
6
votes
1 answer

Executing DynamicExpression with unknown types

If anyone is very familar with the Linq.Dynamic namespace I could use some help -- couldn't find any indepth resources on the internet. Basically I'm using DynamicExpression.ParseLambda to create an expression where the type is not known at compile…
sean
  • 78
  • 1
  • 4
1
2
3
38 39