简体   繁体   中英

Get value from Database to Linq where clause

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);

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.

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 queriesby 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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM