Search code examples
c#sqlsql-serverlinqlinq-to-entities

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

Solution

  • 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