Using a line where a sentence with PredicateBuilder in LinqPad to create a dynamic where clause

I am trying to create a dynamic query in LinqPad with PredicateBuilder enabled.

First, create a line that matches the where clause for a query like "(orderid> 100 AND customerid <= 100)", and then try to use this line when building a LINQ query with PredicateBuilder. The dynamic query is represented by the variable "dynamicResult" in the code at the end of this message. The query is in the Orders table of the Northwind database in SQL Server 2008 R2.

The query throws this error in LinqPad when I try to execute it:

Cannot implicitly convert type 'string' to 'System.Linq.Expressions.Expression>

Question: How to use a filter that is a string of the type (orderid> 100 AND customerid <= 100) 'with PredicateBuilder? I had "C # Statementments" selected from LinqPad when trying to execute the code below.

I am trying to dynamically build a where clause for a LINQ query.

int? orderParam = 100; string orderOperator = ">="; string linqFilter = ""; linqFilter= String.Format("{0} {1} {2}", "o.OrderID", orderOperator, orderParam); linqFilter.Dump(); var predicate = PredicateBuilder.False<Orders>(); predicate = (linqFilter); var dynamicResult = from o in Orders.Where(predicate) select o; dynamicResult.Dump(); 
0
source share
1 answer

Ok try something like this.

  var predicate = PredicateBuilder.False<Orders>(); predicate = predicate.And(o => o.OrderID >= 100); var dynamicResult = from o in Orders.Where(predicate) select o; 

As you said, you used the linqfilter line. this means that you need to dynamically build the expression. So, for this, here is one good article in codeproject . In this article, you will find the Dynamic Location section. You definitely get a hint from this section.

+2
source

Source: https://habr.com/ru/post/1209734/


All Articles