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
4
votes
3 answers

How can I use dynamic LINQ in my asp.net project?

How can I use dynamic LINQ in Visual Studio 2008? I'm trying to use using System.linq.Dynamic but there is no intellisense. How can I use it?
Penguen
  • 16,836
  • 42
  • 130
  • 205
4
votes
1 answer

In System.Linq.Dynamic.Select(), how to deal with a null object reference in a simple way?

I am using dynamic LINQ (System.Linq.Dynamic)(you may find description here, http://dynamiclinq.azurewebsites.net/GettingStarted). The following statement works well Products.Select("new(ProductName, CategoryID.CategoryName as CategoryName)"); But…
Ying
  • 526
  • 4
  • 16
4
votes
1 answer

Dynamic query to immediate execute?

I am using the MSDN Dynamic linq to sql package. It allows using strings for queries. But, the returned type is an IQueryable and not an IQueryable. I do not have the ToList() method. How can I this immediate execute without manually enumerating…
Curtis White
  • 6,213
  • 12
  • 59
  • 83
4
votes
2 answers

Creating a dynamic Linq select clause from Expressions

Let's say I have defined the following variables: IQueryable myQueryable; Dictionary>> extraFields; // the dictionary is keyed by a field name Now, I want to tack on some dynamic fields to the…
Shaul Behr
  • 36,951
  • 69
  • 249
  • 387
4
votes
1 answer

Differences between System.Linq.Dynamic, EntitySQL and Expression Trees

I am currently working on a large project that uses Entity Framework extensively. Part of the functionality we have implemented is dynamic querying (Filter/Sort) of the various data models based on user-supplied filters. To achieve this I ended up…
4
votes
1 answer

How to do the following in Dynamic Linq using System.Linq.Dynamic namespace

I just want to know how to do the following in Dynamic Linq using System.Linq.Dynamic namespace var query2 = db.Customers.Select( cust => new Customer { FirstName = cust.FirstName, …
emem
  • 41
  • 1
  • 3
4
votes
2 answers

How to Type Cast dynamically to string while using Contains in Dynamic LINQ?

I want to use Dynamic LINQ Query to Search with some text in all Properties in a class. i am using following function to create expression. I am passing property name and search text to the method. In that method if the property type is String then…
Kartheek
  • 281
  • 1
  • 6
  • 21
4
votes
6 answers

LINQ Dynamic Expression API, predicate with DBNull.Value comparison

I have an issue using the Dynamic Expression API. I cannot seem to compare a DataTable field against DBNull.Value. The API is supposed to be able to "support static field or static property access. Any public field or property can be accessed.". …
Richard Anthony Hein
  • 10,550
  • 3
  • 42
  • 62
4
votes
1 answer

How to build a nested query with the dynamic LINQ library

How can I build the following LINQ query with the Dynamic Linq library (System.Linq.Dynamic)? var roles = rolesCollection.Where(r => r.AssignedUsers.Where(u => u.Name.FirstName == "Patrick").Count() > 0); rolesCollection and AssignedUsers are…
Patrick Koorevaar
  • 1,219
  • 15
  • 24
4
votes
1 answer

Validating ASP.NET MVC 3 controller's action params when using Dynamic Expressions API

I have a standard ASP.NET MVC 3 Controller with an action that has following signature: public ActionResult Index(int? page, string sort, string sortDir) My view is using WebGrid so parameters are produced by it automatically. Next I use Dynamic…
zam6ak
  • 7,229
  • 11
  • 46
  • 84
3
votes
5 answers

Converting Where from LINQ clause to dynamic LINQ

I want to go from var selectData = (from i in data where i.Name == "Bob1" select i); To var selectData = (from i in data select…
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
3
votes
3 answers

Dynamic linq: Is there a way to access object data by index?

I need some in-memory filtering with Dynamic Linq. My objects have only a indexer: public object this[int index] { } The access to my data is like: object[0], object[1],... So my query is like this: // get FilterText from user at runtime // eg.…
Schorsch
  • 137
  • 1
  • 12
3
votes
2 answers

HowTo use Dynamic linq with an index instead of a property name

I have an ICollection with objects: private ObservableCollection items; The viewItems have no properties. The data will be accessed via an index with public object this[int index] { get{ .... } set {....} } I have a geneal class…
Schorsch
  • 137
  • 1
  • 12
3
votes
2 answers

Dynamic LINQ. No property or field 'FieldName' exists in type 'ClassName'

My LINQ query is as follows Dim Query = From t In New XPQuery(Of xUser)(Xpo.Session.DefaultSession) .Where("Name=John").Select("new (Name as FirstName)") Unfortunately i get the error No property or field 'John' exists in type 'xUser' Of course no…
OrElse
  • 9,709
  • 39
  • 140
  • 253
3
votes
1 answer

Type arguments cannot be inferred from usage, Can anyone help please?

I am trying to use Dynamic Linq (the one published by ScottGu) to do a GroupBy (lambda expression). I have an object. And i am doing AsQueryable. THis is what i have.. var result = Items.AsQueryable().GroupBy("DeliveryDate"); but it gives me an…
Martin
  • 23,844
  • 55
  • 201
  • 327