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
6
votes
1 answer

Entity Framework + DayOfWeek

Using the System.Linq.Dynamic (managed here https://github.com/kahanu/System.Linq.Dynamic ), I am trying to capture the DayOfWeek field found on the DateTime for aggregation purposes using Entity Framework 6 (or greater). Previously asked for…
TravisWhidden
  • 2,142
  • 1
  • 19
  • 43
6
votes
5 answers

C# DynamicLinq where clause with Any()

I want to run dynamic linq with a string where clause like this: query = db.Customers.Where("Categories.Any(Code == 'Retail')"); Customer entity has collection of categories class Customer { public List Categories {get;set;} …
Daler
  • 814
  • 14
  • 28
6
votes
1 answer

Creating dynamic expression for entity framework

I've created a generic expression builder that builds up a predicate based on collection of conditions. I pass the predicate to a generic method in the repository. I think that expression builder works fine and creates the desired predicate although…
ssmsexe
  • 209
  • 2
  • 4
  • 10
6
votes
1 answer

Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda

I'm Using Dynamic Linq library and there is Source code and basic docu and the Nuget version PM> Install-Package DynamicLINQ I'm trying to construct a where clause that involves Guids I have tried with the string "Id == @0". The parameter array…
phil soady
  • 11,043
  • 5
  • 50
  • 95
6
votes
4 answers

Null Reference Exception in a Dynamic LINQ Expression

I am using the Dynamic Linq Library / Sample from Microsoft to do ordering on a list. So for example I have the following C# code: myGrid.DataSource=repository.GetWidgetList() .OrderBy(sortField + " " + sortDirection).ToList(); I have a…
JoshBerke
  • 66,142
  • 25
  • 126
  • 164
6
votes
5 answers

Dynamic LINQ on a collection?

I've a project which ask me to do such a BIG search engine but which is all dynamic. I mean I can have about 0 to 9 main "group" which have inside something like an infinite possibility of "where" with "OR" or "AND". First thing we think was to use…
Simon Dugré
  • 17,980
  • 11
  • 57
  • 73
6
votes
1 answer

Dynamic LINQ Compare Dates in Entity Framework

I am using the dynamic linq library with entity framework and want to compare dates. I have succesfully extended the library based on the following SO article here However what I can not seem to do is to get the library to compare just the date…
Cragly
  • 3,554
  • 9
  • 45
  • 59
6
votes
2 answers

Issue with dynamic LINQ queries containing brackets

I'm using the Dynamic Linq Library (this one) in my .NET MVC application to query a SQL Server database. It's all working fine so far. However, the Dynamic Linq Library gives an "Expression expected" error whenever I use square brackets to designate…
Chris
  • 435
  • 3
  • 9
5
votes
4 answers

Dynamic LINQ (to entities) Where with nullable DateTime column

I have been banging my head on this problem for sometime. There are some similar cases, but the solutions weren't applicable on my case. I have a method that returns filter query in string format. The method has logic for different data types, sets…
Tx3
  • 6,796
  • 4
  • 37
  • 52
5
votes
2 answers

How can I sort a list based on a user's selections in ASP.NET MVC?

I have a list of customers that can be sorted by anywhere from 1 to 6 fields based on a user's selection. The sort fields can be in any order. If I know the fields and the sequence ahead of time, sorting is easy: customers = customers …
Jon Crowell
  • 21,695
  • 14
  • 89
  • 110
5
votes
2 answers

How dynamic can I make my LINQ To SQL Statements?

I have the need to construct a LINQ To SQL statement at runtime based on input from a user and I can't seem to figure out how to dynamically build the WHERE clause. I have no problem with the following: string Filters =…
mcass20
  • 1,668
  • 1
  • 22
  • 39
5
votes
0 answers

What's the most efficient way to search text of any data type in EF6?

I try to dynamically create some LINQ query for searching data in all columns. Currently, I use the following code to build dynamic LINQ query. However, it's quite buggy when deals with complex column. var type =…
user94893
5
votes
2 answers

C# LINQ - How to build Group By clause dynamically

I am working on the application where user can select columns he/she wants to see on the screen and which columns to group by or aggregate. So, in my LINQ section I should actually pass variables that hold column names to both group by and…
superconsultant
  • 231
  • 2
  • 6
  • 20
5
votes
2 answers

Dynamic Linq help, different errors depending on object passed as parameter?

I have an entityDao that is inherbited by everyone of my objectDaos. I am using Dynamic Linq and trying to get some generic queries to work. I have the following code in my generic method in my EntityDao : public abstract class…
SventoryMang
  • 10,275
  • 15
  • 70
  • 113
5
votes
5 answers

Dynamic linq Building Expression

I need to create a dynamic linq expression for a dynamic search.The basic search is working but it fails to work with collection. I am able to get the book's title and author but fails to get the required page heading. I get the exception in line…
Binod
  • 313
  • 1
  • 2
  • 12
1 2
3
38 39