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)

527 questions
56
votes
10 answers

LINQ : Dynamic select

Consider we have this class : public class Data { public string Field1 { get; set; } public string Field2 { get; set; } public string Field3 { get; set; } public string Field4 { get; set; } public string Field5 { get; set;…
Unforgiven
  • 1,719
  • 5
  • 18
  • 18
44
votes
2 answers

Is Injection Possible through Dynamic LINQ?

Using the Dynamic LINQ library (link), is it vulnerable to injection? and (if so) how can this be protected against? Some background from Security Considerations (Entity Framework): LINQ to Entities injection attacks: Although query composition is…
Seph
  • 7,864
  • 10
  • 58
  • 86
37
votes
1 answer

Converting Expression to String

I need a way to recreate dynamically generated reports at some point in the future. Long story short, I need to store a specific linq query (different for each report) into database and then execute the query with dynamic Linq later on. This is all…
user302845
31
votes
4 answers

Dynamic LINQ - Is There A .NET 4 Version?

I'm looking to use LINQ for some searching routines and wanted to have some dynamic where clauses. So, for example, if a user wants to search by city or search by state, I would have a dynamic LINQ Where<> call instead of creating two strongly…
David Hoerster
  • 27,332
  • 6
  • 63
  • 99
24
votes
1 answer

How do I do a left outer join with Dynamic Linq?

I am trying to mimick the left outer join here but using dynamic linq extension methods. What i have: public static IQueryable SelectMany(this IQueryable source, string selector, string resultsSelector, params object[] values) { if (source…
jaywayco
  • 4,536
  • 3
  • 23
  • 40
21
votes
4 answers

System.LINQ.Dynamic: Select(" new (...)") into a List (or any other enumerable collection of )

Say I have a DataTable with four columns, Company (string), Fund (string), State (string), Value(double): table1.Rows.Add("Company 1","Fund 1","NY",100)); table1.Rows.Add("Company 2","Fund 1","CA",200)); table1.Rows.Add("Company 3","Fund…
Brad
15
votes
5 answers

How to use Dynamic LINQ (System.Linq.Dynamic) for LIKE operation?

Can any body tell me how can I use a LIKE operator using System.Linq.Dynamic? I need to add more than one LIKE expression in my dynamic where query /* var query = db.Customers. Where("CityName Like @0 or CityName Like @1", "London", "USA") */ var…
Kiarash
  • 1,351
  • 2
  • 12
  • 20
14
votes
5 answers

Is it possible to accelerate (dynamic) LINQ queries using GPU?

I have been searching for some days for solid information on the possibility to accelerate LINQ queries using a GPU. Technologies I have "investigated" so far: Microsoft Accelerator Cudafy Brahma In short, would it even be possible at all to do…
Johan
  • 237
  • 1
  • 10
14
votes
1 answer

Exception using OrElse and AndAlso expression methods

I am trying to build an expression tree programmatically. I have in my input, a list of condition classes which have the following form: public class Filter { public string field { get; set; } public string operator { get; set; } public…
Lorenzo
  • 28,103
  • 43
  • 117
  • 208
14
votes
3 answers

How to use GroupBy using Dynamic LINQ

I am trying to do a GroupBy using Dynamic LINQ but have trouble getting it to work. This is some sample code illustrating the problem: List listAlbums = new List(); for (int i = 0; i < 5000; i++) { dtoMyAlbum album = new…
Marcel
  • 2,048
  • 6
  • 29
  • 47
13
votes
4 answers

C# Dynamic Linq: Implement "Like" in The Where Clause

So I want to make a general sorter for my data. I have this code to get data from the database which will extract the data only which contains value. using System.Linq.Dynamic; public static IQueryable SortList(string searchString, Type…
Alvin Stefanus
  • 1,163
  • 13
  • 31
10
votes
1 answer

Dynamic linq and operator overloads

Consider the code below: var vectorTest = new Vector2(1, 2) + new Vector2(3, 4); // Works var x = Expression.Parameter(typeof(Vector2), "x"); var test = System.Linq.Dynamic .DynamicExpression.ParseLambda(new[] { x }, null, "x = x +…
George Duckett
  • 29,922
  • 7
  • 92
  • 154
10
votes
3 answers

Dynamic LINQ with direct user input, any dangers?

I have a table in a ASP.NET MVC application that I want to be sortable (serverside) and filterable using AJAX. I wanted it to be fairly easy to use in other places and didn't feel like hardcoding the sorting and filtering into query expressions so I…
JulianR
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,857
  • 3
  • 25
  • 34
10
votes
4 answers

Execution-Deferred IQueryable from Dynamic Linq?

I am using Dynamic Linq to perform some queries (sorry but it's my only option). As a result, I am getting an IQueryable instead of an IQueryable. In my case, I want an IQueryable where Thing is a concrete type. My query is as such: public…
Jaxidian
  • 12,135
  • 7
  • 72
  • 117
1
2 3
35 36