2

I have a table called FILTRE_LINKLER and column's names are

  1. ID
  2. SEF_URL
  3. CONDITIONS

For example,

  • ID=1
  • SEF_URL="test/"
  • CONDITIONS="STOCK>50"`

I want to get CONDITIONS part to linq where clause.

var product = (from d in db.PRODUCTS
                       where *CONDITIONS from DB*
                       select new ProductModel
                       {
                           Description= d.DESCRIPTION,
                           Brand= d.BRANDS.BRAND,
                           SefUrl = sef_url,
                           Name= d.NAME,

                       });

I try to that:

var query = db.FILTRE_LINKLER.Select(x => x.CONDITIONS);
 var product = (from d in db.PRODUCTS
                       where query
                       select new ProductModel
                       {
                           Description= d.DESCRIPTION,
                           Brand= d.BRANDS.BRAND,
                           SefUrl = sef_url,
                           Name= d.NAME,

                       });

But I have an error that is Cannot implicitly convert type 'System.Linq.IQueryable' to bool.

I edited because "Problem Solved". For solution:

Download Install-Package System.Linq.Dynamic -Version 1.0.7 (said me @StepUp) then add to class

using System.Linq.Dynamic;

then following code like that,

 var whereCondition = db.FILTRE_LINKLER.Select(x => x.CONDITIONS).FirstOrDefault();
var product = (from d in db.PRODUCTS
                       where query
                       select new ProductModel
                       {
                           Description= d.DESCRIPTION,
                           Brand= d.BRANDS.BRAND,
                           SefUrl = sef_url,
                           Name= d.NAME,

                       }).Where(whereCondition);
3
  • Are you asking how to add conditions dynamically? Use the extension methods instead of the query syntax and pass whatever condition you want in the .Where() call. eg if (useColor) { query=query.Where(product=>product.Color='Red'); } Commented Jan 22, 2019 at 8:49
  • where this STOCK comes from in your CONDITIONS? Commented Jan 22, 2019 at 8:52
  • Yes, I want to add dynamically but it must come from Database because Conditions can change. It depends on Customers. One of them want to Stock>50 and others want to productName='test'. I want to write a code general usage. Commented Jan 22, 2019 at 8:56

2 Answers 2

1

What you are asking is to dynamically convert the conditions present in the database to where in linq. While there is no magic statement, that says use this. You would have to write an extension that is able to intrepret the conditions written in your database and returns a predicate which you could then use in your where clause.

Sign up to request clarification or add additional context in comments.

2 Comments

can you give me an example ?
@AnilSengul you can pick some ideas from this article - codeproject.com/Tips/582450/…
0

Try to use Dynamic LINQ. This is a sample of code using Dynamic LINQ library:

var query = northwind.Products
                         .Where("CategoryID = 3 AND UnitPrice > 3")
                         .OrderBy("SupplierID");

In your case:

var whereCondition = db.FILTRE_LINKLER.Select(x => x.CONDITIONS).FirstOrDefault();
var product = (from d in db.PRODUCTS
                   select new ProductModel
                   {
                       Description= d.DESCRIPTION,
                       Brand= d.BRANDS.BRAND,
                       SefUrl = sef_url,
                       Name= d.NAME,

                   })
              .Where(whereCondition);

UPDATE:

  1. At first you should download a code to use Dynamic queries by this link.

  2. Then use DynamicQueryable class from the project Dynamic Query

  3. Then use your dynamic queries with IQueryable<T>.

Let me show an example:

var persons = new List<Person>()
{
    new Person(){Id = 1, FirstName = "1"},
    new Person(){Id = 2, FirstName = "2"},
    new Person(){Id = 3, FirstName = "3"}
};
var personWithIdTwo = persons
   .AsQueryable()
   .Where("Id==2");

UPDATE 1:

If you do not want to add class, then you can use an Expression Tree.

An example of an expression tree:

var propName = "STOCK"; // here you assign any value
var constValue = "50";  // here you assign any value
var param = Expression.Parameter(typeof(ProductModel), "p");
var exp = Expression.Lambda<Func<ProductModel, bool>>(
    Expression.GreaterThan(
       Expression.Property(param, propName),
       Expression.Constant(constValue)
    ),
    param
);
var product = (from d in db.PRODUCTS
                   where query
                   select new ProductModel
                   {
                       Description= d.DESCRIPTION,
                       Brand= d.BRANDS.BRAND,
                       SefUrl = sef_url,
                       Name= d.NAME,

                   }).Where(exp);

UPDATE 2:

You can download library Dynamic query through NuGet. So you should not create classes in your project:

enter image description here

8 Comments

did not work. "cannot convert from system.linq.expressions.expression to system.func<Model,bool>" error messages I got.
@AnilSengul Please, see my updated answer. You should create class to work with dynamic queries.
Just I want to add little thing. I have to add class for little thing. Are there other way to do this ?
Property and Constant must be dynamic. These are in same string
Yes it is enough for me. Problem solved. Thanks for answering. I shared solution above with editting my question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.