Converting LINQ expression to expression tree queryable.Where(c => c.Tags != null && searchValues.All(s => c.Tags.Contains(s))); - c#

I have a list of strings string[] searchValues and a LINQ expression
queryable.Where(c => c.Tags != null && searchValues.All(s => c.Tags.Contains(s)));
where .Tags is a List<string>
I want to rewrite it as an expression (filters and sorting orders are coming from UI as strings, and I wanted to create a generic method to convert it to expressions)
So far I've written this, but it's incorrect.
private static Expression GetFilterExpressionIListString2(MemberExpression memberExp, string[] values)
{
var anyMethod = typeof(Enumerable).GetMethods().First(m => m.Name == "Any" && m.GetParameters().Length == 2);
var specificAnyMethod = anyMethod.MakeGenericMethod(typeof(string));
Expression<Func<List<string>, bool>> lambda = s => values.All(v => s.Contains(v));
var anyExp = Expression.Call(specificAnyMethod, memberExp, lambda);
var notNullExp = Expression.NotEqual(memberExp, Expression.Constant(null));
var andExp = Expression.AndAlso(notNullExp, anyExp);
return andExp;
}

Related

How to generate an Expression to query a match on any members of an entity or of its related Entities?

I have the following code that I'm using to perform searches on my efcore data. Since the data set it so huge, I had to start using dynamic / generic types. I've been able to query on entity level properties, but I'm struggling to query entities that would have been defined as .Include(x => x.SomeInclusionEntity)
I've included my working code, as well as the second labelled "THIS SECTION DOESNT WORK" to show my ideas. I know its not perfect, but it works reasonably well for our internal use cases. Most people just use basic string searches for the same things over and over.
public IQueryable<T> GetBySearchTerm(IQueryable<T> queryable, string search)
{
T thisEntityBaseModel = new T();
IEntityType set = _dbContext.Model.GetEntityTypes().First(x => x.ClrType.Name.ToUpper() == thisEntityBaseModel.ModelName.ToUpper());
List<Expression<Func<T, bool>>> predicateArray = new List<Expression<Func<T, bool>>>();
MethodInfo containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
foreach (IProperty columnProp in set.GetProperties()) {
if (columnProp.ClrType == typeof(string)) {
// Define the parameter
ParameterExpression xParam = Expression.Parameter(typeof(T), "x");
// Create the expression representing what column to do the search on
MemberExpression colExpr = Expression.Property(xParam, columnProp.Name);
// Create a constant representing the search value
ConstantExpression constExpr = Expression.Constant(search);
// Generate a method body that represents "column contains search"
MethodCallExpression lambdaBody = Expression.Call(colExpr, containsMethod, constExpr);
// Convert the full expression into a useable query predicate
Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(lambdaBody, xParam);
predicateArray.Add(lambda);
}
}
/* THIS SECTION DOESNT WORK===========================================================
// Traverse declared navigation
foreach (INavigation declaredNavigation in set.GetDeclaredNavigations())
{
// These are the navigations included by EFcore that aren't part of the data model. Search them too
IEnumerable<IProperty> x = declaredNavigation.TargetEntityType.GetProperties();
foreach (IProperty columnProp in x)
{
if (columnProp.ClrType == typeof(string))
{
// Define the parameter
ParameterExpression xParam = Expression.Parameter(declaredNavigation.ClrType, "z");
// Create the expression representing what column to do the search on
MemberExpression colExpr = Expression.Property(xParam, columnProp.Name);
// Create a constant representing the search value
ConstantExpression constExpr = Expression.Constant(search);
// Generate a method body that represents "column contains search"
MethodCallExpression lambdaBody = Expression.Call(colExpr, containsMethod, constExpr);
// Convert the full expression into a useable query predicate
LambdaExpression zz = Expression.Lambda(lambdaBody, xParam);
//Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(lambdaBody, xParam);
//predicateArray.Add(lambda);
}
}
}
THIS SECTION DOESNT WORK===========================================================*/
// This performs an "OR" method on the predicates, since by default it wants to do "AND"
var predicate = PredicateBuilder.False<T>();
foreach (Expression<Func<T, bool>> expression in predicateArray) {
predicate = predicate.Or(expression);
}
// Process the ors
return (queryable.Where(predicate));
}
If I get it right, your target is to generate a query that will be equivalent to something like this:
users.Where(u => u.Name.Contains("Foo") ||
u.Alias.Contains("Foo") ||
... ||
u.City.CityName.Contains("Foo") ||
... ||
u.Pets.Any(p => p.Name.Contains("Foo") ||
...
);
In the part that did not work, in the line
MemberExpression colExpr = Expression.Property(xParam, columnProp.Name);
I think it generates u.CityName instead of u.City.CityName.
You need to get the property name associated with the INavigation (in my example, it's City), and inject it in the lambda.
To retrieve the navigation property name just use INavigation.Name
Here is a working example of this implementation:
public static class DbSetExtension
{
public static IQueryable<T> DeepSearch<T>(this DbSet<T> dbSet, string search)
where T : class
{
return DeepSearch(dbSet, dbSet, search);
}
public static IQueryable<T> DeepSearch<T>(this IQueryable<T> queryable, DbContext dbContext, string search)
where T : class
{
var set = dbContext.Set<T>();
return DeepSearch(queryable, set, search);
}
public static IQueryable<T> DeepSearch<T>(this IQueryable<T> queryable, DbSet<T> originalSet, string search)
where T : class
{
var entityType = originalSet.EntityType;
var containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) })!;
// Ack to retrieve Enumerable.Any<>(IEnumerable<>, Func<,>)
var anyMethod = typeof(Enumerable)
.GetMethods()
.Single(m => m.Name == "Any" &&
m.GetParameters().Length == 2);
// {x}
var xParam = Expression.Parameter(typeof(T), "x");
// {search}
var constExpr = Expression.Constant(search);
// {x.Name.Contains(search)} list
var xDotNames = entityType.GetProperties()
.Where(p => p.ClrType == typeof(string))
.Select(p => Expression.Property(xParam, p.Name))
.Select(e => (Expression)Expression.Call(e, containsMethod, constExpr));
// {x.Navigation.Name.Contains(search)} list
var xDotOtherDotNames = entityType.GetDeclaredNavigations()
.Where(n => !n.IsCollection)
.SelectMany(n => n.TargetEntityType
.GetProperties()
.Where(p => p.ClrType == typeof(string))
.Select(p => NestedProperty(xParam, n.Name, p.Name)))
.Select(e => Expression.Call(e, containsMethod, constExpr));
// {x.Navigations.Any(n => n.Name.Contains(search))} list
var xDotOthersDotNames = entityType.GetDeclaredNavigations()
.Where(n => n.IsCollection)
.SelectMany(n =>
{
var nType = n.TargetEntityType.ClrType;
// Enumerable.Any<NType>
var genericAnyMethod = anyMethod.MakeGenericMethod(nType);
// {n}
var nParam = Expression.Parameter(nType, "n");
// {x.Navigations}
var xDotNavigations = Expression.Property(xParam, n.Name);
return n.TargetEntityType
.GetProperties()
.Where(p => p.ClrType == typeof(string))
.Select(p =>
{
// {n.Name}
var nDotName = Expression.Property(nParam, p.Name);
// {n.Name.Contains(search)}
var contains =
Expression.Call(nDotName, containsMethod, constExpr);
// {n => n.Name.Contains(search)}
var lambda = Expression.Lambda(contains, nParam);
// {Enumerable.Any(x.Navigations, n => n.Name.Contains(search))
return Expression.Call(null, genericAnyMethod, xDotNavigations, lambda);
});
})
.ToList();
// { || ... }
var orExpression = xDotNames.Concat(xDotOtherDotNames)
.Concat(xDotOthersDotNames)
.Aggregate(Expression.OrElse);
var lambda = Expression.Lambda<Func<T, bool>>(orExpression, xParam);
return queryable.Where(lambda);
}
private static Expression NestedProperty(Expression expression, params string[] propertyNames)
{
return propertyNames.Aggregate(expression, Expression.PropertyOrField);
}
}

Expression tree for groupby with where clause and than select

From UI dynamic column are coming as parameter in API and based on the parameter I have to fetch data from database.
Example : In the below code, based on the column if condition linq query is being executed. Now I want to make it generic so that it serve if new column condition come in future.
public List<string> GetFilteredTypeAhead(string searchText,string searchForRole,int fiscalyear,int fiscalPeriod)
{
if (searchForRole == "column1")
{
var accounts = (from a in _context.Account
where a.column1.StartsWith(searchText) && a.FiscalPeriod == fiscalPeriod && a.FiscalYear ==fiscalyear
group a.column1 by a.column2 into g
select g.Key).ToList();
return accounts;
}
else if(searchForRole == "column2")
{
var accounts = (from a in _context.Account
where a.column2.StartsWith(searchText) && a.FiscalPeriod == fiscalPeriod && a.FiscalYear == fiscalyear
group a.column2 by a.column2 into g
select g.Key).ToList();
return accounts;
}
else if (searchForRole == "column3")
{
var accounts = (from a in _context.Account
where a.column3.StartsWith(searchText) && a.FiscalPeriod == fiscalPeriod && a.FiscalYear == fiscalyear
group a.column3 by a.column3 into g
select g.Key).ToList();
return accounts;
}
else if (searchForRole == "column4")
{
var accounts = (from a in _context.Account
where a.column4.StartsWith(searchText) && a.FiscalPeriod.Equals(fiscalPeriod) && a.FiscalYear.Equals(fiscalyear)
group a.column4 by a.column4 into g
select g.Key).ToList();
return accounts;
}
else
{
return new List<string>();
}
}
To convert it to generic. I created a expression tree.
static IQueryable<T> ConvertToExpression<T>(IQueryable<T> query, string propertyValue, PropertyInfo propertyInfo, int fiscalyear, int fiscalPeriod)
{
ParameterExpression e = Expression.Parameter(typeof(T), "e");
MemberExpression m = Expression.MakeMemberAccess(e, propertyInfo);
ConstantExpression c = Expression.Constant(propertyValue, typeof(string));
MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
Expression call = Expression.Call(m, mi, c);
PropertyInfo propertyInfoFiscalPeriod = typeof(T).GetProperty("FiscalPeriod");
MemberExpression memberPropertyFiscalPeriod = Expression.Property(e, propertyInfoFiscalPeriod);
ConstantExpression right = Expression.Constant(fiscalPeriod);
Expression equalsFiscalPeriod = Expression.Equal(memberPropertyFiscalPeriod, Expression.Convert(right, typeof(Int16)));
PropertyInfo propertyInfoFiscalYear = typeof(T).GetProperty("FiscalYear");
MemberExpression memberPropertyFiscalYear = Expression.Property(e, propertyInfoFiscalYear);
right = Expression.Constant(fiscalyear);
Expression equalsFiscalYear = Expression.Equal(memberPropertyFiscalYear, Expression.Convert(right, typeof(Int16)));
Expression combineExpression = Expression.And(equalsFiscalPeriod, equalsFiscalYear);
Expression predicateBody = Expression.And(call, combineExpression);
Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(predicateBody, e);
return query.Where(lambda);
}
And To call it I used code like below
"searchForRole" comes as parameter in as "column1","column2" etc
PropertyInfo propertyInfo = typeof(Account).GetProperty(searchForRole);
IQueryable<Account> query = _context.Account;
query = ConvertToExpression(query, searchText, propertyInfo,fiscalyear,fiscalPeriod);
var list = query.ToList();
Now this is working fine but the result having duplicate records. I wanted to have some distinct or group by on passed parameter column. In Simple words I wanted to remove if condition and make my search method generic. Please help.
It's possible, but IMHO it's better to keep the dynamic parts at minimum and use the C# compile time safety as much as possible.
The sample query in question
var accounts = (from a in _context.Account
where a.column1.StartsWith(searchText) && a.FiscalPeriod == fiscalPeriod && a.FiscalYear ==fiscalyear
group a.column1 by a.column1 into g
select g.Key).ToList();
can be rewritten as follows
var accounts = _context.Account
.Where(a => a.FiscalPeriod == fiscalPeriod && a.FiscalYear == fiscalyear)
.Select(a => a.column1)
.Where(c => c.StartsWith(searchText))
.Distinct()
.ToList();
As you can see, the only dynamic part is a => a.column1 of type Expression<Func<Account, string>>. So all you need is a method like this:
static Expression<Func<T, M>> MemberSelector<T>(string name)
{
var parameter = Expression.Parameter(typeof(T), "e");
var body = Expression.PropertyOrField(name);
return Expression.Lambda<Func<T, M>>(body, parameter);
}
and to replace
.Select(a => a.column1)
with
.Select(MemberSelector<Account, string>(searchForRole))

Reflected property(dynamic property) inside Linq lambda

How can i replace x.Demographic.AgeRange with any other field?
Eg
var field_to_check = "Country";
x.Demographic.ReflectedProperty(field_to_check)=="USA"
var field_to_check = "AgeRnage";
x.Demographic.ReflectedProperty(field_to_check)=="20-30"
I Tried with a refelcted property. but cant succeed.
Favourability = db.Questions
.OrderByDescending(x => x.Responces.Count(y => y.Responseval == Constants.options.Agree || y.Responseval == Constants.options.Tend_to_Agree))
.Select(z => new
{
z.QuestionTitle,
Count = z.Responces.Where(x =>
x.Demographic.AgeRange == repval &&
(x.Responseval == Constants.options.Agree || x.Responseval == Constants.options.Tend_to_Agree)
)
.Count()
})
.Select(z => new
{
z.QuestionTitle,
z.Count,
Perc = ((z.Count / totresponcecount) * 100)
}
)
.ToList();
so that i can write only one linq statement as dynamic filtering rather than switch statements for all required properties.
You can construct the expression tree from multiple different ones. Use LinqKit, and write:
Expression<Func<Demography, string>> fieldSpec = d => d.AgeRange;
And then in your expession:
var exp = db.Questions.AsExpadable() ... fieldSpec.Expand(x.Demographic) == repval ...
Now all you need to do is construct the fieldSpec dynamically, which is an excercise for you :)

LINQ to Entities does not recognize the method Boolean ContainsAny

Error
LINQ to Entities does not recognize the method 'Boolean
ContainsAny(System.String, System.String[])' method, and this method
cannot be translated into a store expression.
Code
var predicate = FilterClients(clientRequest);
clients = db.Clients.Where(predicate).Include(x => x.Organization)
.Include(x => x.Department).ToList();
private Expression<Func<Client, bool>> FilterClients(ClientRequest clientRequest)
{
var predicate = PredicateBuilder.True<Client>();
if (clientRequest.IsBySearchPatternFullName)
{
var searchPatternFullName = clientRequest.SearchPatternFullName.Trim();
var matches = Regex.Matches(searchPatternFullName, #"\w+[^\s]*\w+|\w");
var words = new List<string>();
foreach (Match match in matches)
{
var word = match.Value;
words.Add(word);
}
var wordArray = words.ToArray();
predicate = predicate.And(x => x.LastName.ContainsAny(wordArray) || x.FirstName.ContainsAny(wordArray) || x.MiddleName.ContainsAny(wordArray));
}
return predicate;
}
There is similar question C# LINQ to Entities does not recognize the method 'Boolean'
But I would like to optimize this part somehow
predicate = predicate.And(x => x.LastName.ContainsAny(wordArray) || x.FirstName.ContainsAny(wordArray) || x.MiddleName.ContainsAny(wordArray));
Any clue people?
Oh! I just did it!
foreach (Match match in matches)
{
var word = match.Value;
predicate = predicate.And(x => x.LastName.Contains(word) || x.FirstName.Contains(word) || x.MiddleName.Contains(word));
}
You can query against list of word in following way :
predicate = predicate.And(x => wordArray.Contains(x.LastName) || wordArray.Contains(x.FirstName) || wordArray.Contains(x.MiddleName));

Entity, Contains or intersect, is this query possible?

I have a list of string retreived this way :
List<string> keyWords = db.MotCleRecherche.Select(t => t.MotClé).ToList();
I also have a query that takes many parameters to be executed :
object = db.DAapp.Where(t => t.CODE_ART.StartsWith(s) && t.DATE_CREAT >= debut && t.DATE_CREAT < fin).ToList()
now... I want to add this kind of condition :
db.DAapp.Where(t => t.DESC_ART.ToLower().Contains(keywords.ToLower()))
or
db.DAapp.Where(t => t.DESC_ART.ToLower().Intersect(keywords.ToLower()))
I guess you could see it comming... I can't figure how to really make this work... all i know is considering a list X filed and Y list filled:
X.Intersect(Y).Any()
will return true if there is something equal... but DESC_ART is just ONE long string and i want to know if some of my keywords are in there
I agree with Stephen that you should cast the keyWords to lower first before comparing. But if you really need to do this with linq you can do something like this.
var result = db.DAapp.Where(t => keywords.Any(keyword=> string.Equals(keyword,t.DESC_ART, StringComparison.InvariantCultureIgnoreCase )));
This will cause a to lower to get called on each string every iteration of your linq loop so its expensive.
First add this to your project (for example to your controller):
static Expression<Func<T, bool>> AnyOf<T>(
params Expression<Func<T, bool>>[] expressions)
{
if (expressions == null || expressions.Length == 0) return x => false;
if (expressions.Length == 1) return expressions[0];
var body = expressions[0].Body;
var param = expressions[0].Parameters.Single();
for (int i = 1; i < expressions.Length; i++)
{
var expr = expressions[i];
var swappedParam = new SwapVisitor(expr.Parameters.Single(), param)
.Visit(expr.Body);
body = Expression.OrElse(body, swappedParam);
}
return Expression.Lambda<Func<T, bool>>(body, param);
}
class SwapVisitor : ExpressionVisitor
{
private readonly Expression from, to;
public SwapVisitor(Expression from, Expression to)
{
this.from = from;
this.to = to;
}
public override Expression Visit(Expression node)
{
return node == from ? to : base.Visit(node);
}
}
I find this from stackoverflow. now you can create desired query as below :
var filters = new List<Expression<Func<Models.DAapp, bool>>>();
foreach (var st in keyWords)
filters.Add(d => d.DESC_ART.ToLower().Contains(st.ToLower()));
var lambda = AnyOf(filters.ToArray());
var q = db.DAapp.Where(t =>
t.CODE_ART.StartsWith(s)
&& t.DATE_CREAT >= debut
&& t.DATE_CREAT < fin
);
q = q.Where(lambda);
var res = q.ToList();
Please be noticed that, this solution creates only one select query with multiple where expressions. which is more efficient that other solutions like below that contains multiple select queries inside where clause :
var q = db.DAapp.Where(t =>
t.CODE_ART.StartsWith(s)
&& t.DATE_CREAT >= debut
&& t.DATE_CREAT < fin
&& keyWords.Any(k => t.DESC_ART.ToLower().Contains(k.ToLower()))
);

Categories

Resources