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

how I can get insert method more flexible using dynamic linq?

I want to write some entity into database but there are a lot of entities I like to write something generally for them now I know this : using(var dbContext = new myEntity()) { db.EntityName.AddToObject(newEntity); db.SubmitChanges(); } I…
kamiar3001
  • 2,646
  • 4
  • 42
  • 78
0
votes
3 answers

Access Linq result contained in a dynamic class

I'm using DbLinq which should be the equivalent of Linq2SQL for this question. I'm need to generate a Linq2SQL query where I specify the columns I want returned at runtime. I can achieve that using the Dynamic Linq extension methods but I can't work…
sipsorcery
  • 30,273
  • 24
  • 104
  • 155
-1
votes
1 answer

'Parameter count mismatch' Error when trying to insert List to Excel using closedXml InsertData

I am trying to export dynamic list items to excel using ClosedXML. The goal is to allow end user to select the columns to be exported. Since the columns are dynamic, I use.ToDynamicListAsync() of System.Linq.Dynamic.Core name space. The problem is…
Rubeesh
  • 97
  • 1
  • 4
-1
votes
1 answer

Unused dynamic SQL removal

Supose I have the following classes: public class Person { public int Id {get; set;} public string Name {get; set;} public IEnumerable
Address {get; set;} } public class Address { public int Id {get; set;} public string Street…
-1
votes
2 answers

How to create a LINQ group by query with dynamic columns?

I want to group by a datatable by the columns which are present in a List. Moreover I want to sum a column using group by result. How to create a dynamic linq query for this?
Shivani
  • 1
  • 1
-1
votes
2 answers

C# Dynamic LINQ Contains using an array only evaluates the first value

Relatively new to C# and started using Dynamic LINQ to filter a data table adapter using a string. The issue I'm having is that the Where clause only seems to evaluate the first value in the string and none of the others. Here is the code I am…
Lawrence
  • 21
  • 2
-1
votes
1 answer

Linq - Dynamic query that contain DateTime filed

I have search like as below picture: I create query by below code: StringBuilder query = new StringBuilder(); string OrderBy = null; switch (sortOrder) { case "orderCode_desc": OrderBy = " OrderCode desc"; break; case…
Farzaneh Talebi
  • 835
  • 4
  • 22
  • 47
-1
votes
1 answer

Dynamic sorting using Func, IOrderedQueryable>

i want to query result and display in order based on Id,Nam,Phone and Email can anyone help me how to do it. expected result table1result=> table1.OrderBy(c => c.Id).ThenBy(c => c.Name).ThenBy(c => c.table2.Name).ThenBy(c=>c.table2.Phone) below is…
-1
votes
1 answer

Select using System.Linq.Dynamic

I want to select one field from my database like using (MyContext context = new MyContext() ) { MyClass x = context.MyTable.Where("Id =@0","O1").Select(" new MyClass(DatabaseField)").Cast().Single(); } public class MyClass { public…
Avinash
  • 107
  • 1
  • 1
  • 7
-1
votes
1 answer

Using DateTime for comparisons in dynamic LINQ with other type parameters in predicate

My requirement is simple and very common. I did find many struggling to find answers to it but did not get any acceptable solution. I am using EF 6.1 and want to query a table with a predicate which is being created dynamically something like "ABC >…
Deepak Agarwal
  • 458
  • 1
  • 4
  • 18
-1
votes
2 answers

Dynamic Linq 'contains' clause without using placeholder

The syntax given for contains clause is ids = new int[] {1,2,3,4}; dataContext.Table.Where("@0.Contains(id)", ids); But what I want is dataContext.Table.Where("{1,2,3,4}.Contains(id)"); //getting exception here [ERROR] Expression expected (at…
Sujit.Warrier
  • 2,815
  • 2
  • 28
  • 47
-1
votes
1 answer

Reusable LINQ method query

Is it possible to create a reusable LINQ query expression with calls to OrderBy and Where, without applying it to a specific IQueryable? I would like to be able to do something like this: var data = new List(); var builder = new…
Shahin Dohan
  • 6,149
  • 3
  • 41
  • 58
-1
votes
1 answer

Dynamic Linq return wrong result

I want to search address by linq, I use in 2 ways: Linq: _db.Address.Where(address => address.FullName.Contains(text)); Dynamic Linq: _db.Address.Where("FullName.Contains(@0)", text); When I try: -> text value is string (doesnot contain space),…
Stiger
  • 1,189
  • 2
  • 14
  • 29
-2
votes
1 answer

Compile a string representation of a lambda into a lambda

I have the following lambda that works fine: public interface IOhlcv : ITick { decimal Open { get; set; } decimal High { get; set; } decimal Low { get; set; } decimal Close { get; set; } decimal Volume { get; set; } } c1,c2,c3…
Ivan
  • 7,448
  • 14
  • 69
  • 134
-2
votes
2 answers

How to get Predicate from string expressions at runtime

In Winform application using EntityFramework, I implement a generic Search / Filter UI Components using BindingSource of the BaseForm and build search/filter string expression dynamically from user inputs and properties of the DataSource of…
M.Hassan
  • 10,282
  • 5
  • 65
  • 84
1 2 3
38
39