Reputation: 33
I have a table called FILTRE_LINKLER
and column's names are
For example,
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);
Upvotes: 2
Views: 2329
Reputation: 38094
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:
At first you should download a code to use Dynamic queries
by this link.
Then use DynamicQueryable
class from the project Dynamic Query
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:
Upvotes: 0
Reputation: 47774
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.
Upvotes: 1