I have a method that is designed to check if a List of strings is contained within a List of column names in a table. Here is the method:
public static IQueryable<T> ContainsQuery<T>(IQueryable<T> query, IEnumerable<string> propertyNames, IEnumerable<string> propertyValues)
{
ParameterExpression entity = Expression.Parameter(typeof(T), "entity");
MethodInfo containsInfo = typeof(string).GetMethod("Contains", new Type[] { typeof(string) });
MethodInfo toLowerInfo = typeof(string).GetMethod("ToLower", new Type[] {});
BinaryExpression masterExpression = Expression.Or(Expression.Constant(true, typeof(Boolean)), Expression.Constant(true, typeof(Boolean)));
ConstantExpression negativeOne = Expression.Constant(-1, typeof(int));
foreach (var propertyName in propertyNames)
{
foreach (var propertyValue in propertyValues)
{
PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
ConstantExpression valueExpression = Expression.Constant(propertyValue, typeof(string));
ConstantExpression caseInsensitiveComparisonExpression = Expression.Constant(StringComparison.CurrentCultureIgnoreCase, typeof(StringComparison));
MemberExpression propertyExpression = Expression.Property(entity, propertyInfo);
MethodCallExpression propertyLoweredExpression = Expression.Call(propertyExpression, toLowerInfo);
MethodCallExpression valueLoweredExpression = Expression.Call(valueExpression, toLowerInfo);
MethodCallExpression containsExpression = Expression.Call(propertyLoweredExpression, containsInfo, valueLoweredExpression);
masterExpression = Expression.Or(containsExpression, masterExpression);
}
}
Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(masterExpression, entity);
return query.Where(lambda);
}
I am using it like so:
qry = QueryExpressions.ContainsQuery(qry, searchCols, new List<string> {universalSearch});
As I step through the code, qry starts out as an IQueryable select all statement. Then this method gets called and I check lambda and it is a nested list of or's as I expect. When I step out of the method though, qry remains a simple select all statement as before. My lambda expression is not being applied. No matter what I pass in as universalSearch, the qry returns the entire data set.
Thanks in advance.
Here is lambda after the method is finished executing:
{entity => (entity.STAKE_COMMENT.ToLower().Contains("other".ToLower()) Or (entity.NOTES.ToLower().Contains("other".ToLower()) Or (entity.COMMENT_SOURCE.ToLower().Contains("other".ToLower()) Or (entity.STAKE_COMMENT_DATE2.ToLower().Contains("other".ToLower()) Or (entity.COMMENT_STATUS.ToLower().Contains("other".ToLower()) Or (entity.COMMENT_CATEGORY.ToLower().Contains("other".ToLower()) Or (entity.COMMENTTOPIC.ToLower().Contains("other".ToLower()) Or (entity.STAKEHOLDER.ToLower().Contains("other".ToLower()) Or (True Or True)))))))))}
The problem is not that your query is unchanged but that your predicate is flawed.
You have this as predicate:
... lots of stuff ... Or (True Or True)
Every condition that contains or true will always evaluate to true.
Related
I'm writing an extension method that takes a property name as a string and builds an expression tree to order by that column. It needs to support Linq-to-Entities as I'd like the order-by operation to be done in SQL. The method currently works as intended, however I'd now like to prioritise non-null values, i.e. always leave null values at the end of the collection, regardless of the order direction.
Here's the method I have so far:
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string orderBy, bool ascending) {
Type type = typeof(T);
ParameterExpression parameter = Expression.Parameter(type, "p");
PropertyInfo property = type.GetProperty(orderBy);
if (property == null) {
throw new ArgumentException("The given value did not match any available properties.", nameof(orderBy));
}
// Get the property accessor p.SortColumn
Expression propertyAccess = Expression.MakeMemberAccess(parameter, property);
// Get the lambda expression p => p.SortColumn
LambdaExpression orderByExp = Expression.Lambda(propertyAccess, parameter);
// Call the OrderBy(Descending) method with the above lambda expression
MethodCallExpression resultExp = Expression.Call(typeof(Queryable),
ascending ? "OrderBy" : "OrderByDescending", new[] { type, property.PropertyType },
source.Expression, Expression.Quote(orderByExp));
return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(resultExp);
}
If I didn't need to support Linq-to-Entities, I'd write something like this:
source.OrderBy(x => property.GetValue(x) == null).ThenBy/Descending(x => property.GetValue(x));
However, the Expression.Call()... syntax has thrown me as I don't fully understand what's happening behind the scenes there.
How would I go about prepending an OrderBy(value == null) operation to the expression tree?
Just order by equal to null first
var result = list.OrderBy(x => x.Name == null).ThenBy(x => x.Name)
Example
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string propertyName, bool ascending)
{
var propertyInfo = typeof(T).GetProperty(propertyName) ?? throw new ArgumentException("The given value did not match any available properties.", nameof(propertyName));
var parameterExpression = Expression.Parameter(typeof(T), "p");
var propertyAccess = Expression.MakeMemberAccess(parameterExpression, propertyInfo);
var orderByNullLambda = Expression.Lambda(
Expression.Equal(propertyAccess, Expression.Constant(null)),
parameterExpression);
var resultExp = Expression.Call(
typeof(Queryable),
ascending ? "OrderBy" : "OrderByDescending",
new[] {typeof(T), typeof(bool)},
source.Expression,
Expression.Quote(orderByNullLambda));
var orderByPropertyLambda = Expression.Lambda(
propertyAccess,
parameterExpression);
var resultExp2 = Expression.Call(
typeof(Queryable),
ascending ? "ThenBy" : "ThenByDescending",
new[] {typeof(T), propertyInfo.PropertyType},
resultExp,
Expression.Quote(orderByPropertyLambda));
return (IOrderedQueryable<T>) source.Provider.CreateQuery<T>(resultExp2);
}
Usage
var list = new List<Bob>()
{
new() {Name = "Bsd"},
new() {Name = null},
new() {Name = "asd"}
};
var result = list.AsQueryable().OrderBy("Name", true).ToList();
foreach (var item in result)
Console.WriteLine(item.Name ?? "Null");
Result
asd
Bsd
Null
I have a service method called FindAll() that expects a expression/predicate parameter and returns all the matching rows.
Let's say I have a collection of Book objects and I want to get all the books with their names found in a list of strings. I'd have something like this:
var lstNames = { "Book1", "Book2" };
var matchedBooks = myService<Book>.FindAll(x => lstNames.Any(y => x.Name.Equals(y)));
I also have a number of other classes that all have the Name property so I'd like to build a dynamic expression which allows me to do something like:
var matchedObjs = myService<T>.FindAll(x => lstNames.Any(y => x.Name.Equals(y)));
How do I build such a dynamic expression?
Thanks to this answer: How to declare a Linq Expression variable in order to have it processed as a dbParameter
I propose you do this:
static Expression<Func<T, bool>> GetExpr<T> (string name, string value)
{
ParameterExpression param = Expression.Parameter(typeof(T), "x");
Expression prop = Expression.Property(param, name); // this is the property name, e.g. .Name
Expression<Func<string>> valueLambda = () => value; // This is the value for == expression.
Expression lookupExpression = Expression.Equal(prop, valueLambda.Body);
Expression<Func<T, bool>> expr = Expression.Lambda<Func<T, bool>>(lookupExpression, param);
return expr;
}
... or, for .Contains():
static Expression<Func<T, bool>> GetExprContains<T>(string name, string[] value)
{
ParameterExpression param = Expression.Parameter(typeof(T), "x");
Expression prop = Expression.Property(param, name); // this is the property name, e.g. .Name
Expression<Func<string[]>> valueLambda = () => value; // This is the value for .Contains() expression.
var mi =
typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
.FirstOrDefault(x => x.Name == "Contains" && x.GetParameters().Count() == 2)
.MakeGenericMethod(typeof(string)); // Need methodinfo for .Contains(), might want to keep static somewhere
var lookupExpr = Expression.Call(null, mi, valueLambda.Body, prop);
Expression<Func<T, bool>> expr = Expression.Lambda<Func<T, bool>>(lookupExpr, param);
return expr;
}
Tested, works with EF.
As per requirement I want to create a dynamic lambda expression using C#.
For example I want to generate the dynamic query like
Employee. Address[1].City
How can I do this? Please note that the property is a dynamic one.
I have tried this code
var item = Expression.Parameter(typeof(Employee), "item");
Expression prop = Expression.Property(item, "Address", new Expression[] { Expression.Constant[1] });
prop = Expression.Property(prop, "City");
var propValue = Expression.Constant(constraintItem.State);
var expression = Expression.Equal(prop, propValue);
var lambda = Expression.Lambda<Func<Line, bool>>(expression, item);
But it did not work.
Any help would be appreciated.
Thanks.
You "dynamic query" expression (which is not really a query, it's a simple MemberExpression) can be produced as follows:
ParameterExpression param = Expression.Parameter(typeof(Employee), "item");
MemberExpression address = Expression.Property(param, "Address");
BinaryExpression indexedAddress = Expression.ArrayIndex(address, Expression.Constant(1));
MemberExpression city = Expression.Property(indexedAddress, "City"); // Assuming "City" is a string.
// This will give us: item => item.Address[1].City
Expression<Func<Employee, string>> memberAccessLambda = Expression.Lambda<Func<Employee, string>>(city, param);
If you want an actual predicate to use as part of your query, you just wrap the MemberExpression with a relevant compare expression, i.e.
BinaryExpression eq = Expression.Equal(city, Expression.Constant("New York"));
// This will give us: item => item.Address[1].City == "New York"
Expression<Func<Employee, bool>> predicateLambda = Expression.Lambda<Func<Employee, bool>>(eq, param);
In terms of your code: not sure why you're creating a lambda where the delegate type is Func<Line, bool> when the input is clearly expected to be Employee. Parameter type must always match the delegate signature.
EDIT
Non-array indexer access example:
ParameterExpression param = Expression.Parameter(typeof(Employee), "item");
MemberExpression address = Expression.Property(param, "Address");
IndexExpression indexedAddress = Expression.MakeIndex(
address,
indexer: typeof(List<string>).GetProperty("Item", returnType: typeof(string), types: new[] { typeof(int) }),
arguments: new[] { Expression.Constant(1) }
);
// Produces item => item.Address[1].
Expression<Func<Employee, string>> lambda = Expression.Lambda<Func<Employee, string>>(indexedAddress, param);
// Predicate (item => item.Address[1] == "Place"):
BinaryExpression eq = Expression.Equal(indexedAddress, Expression.Constant("Place"));
Expression<Func<Employee, bool>> predicateLambda = Expression.Lambda<Func<Employee, bool>>(eq, param);
I want to use Dynamic LINQ Query to Search with some text in all Properties in a class. i am using following function to create expression. I am passing property name and search text to the method.
In that method if the property type is String then it is working fine. if the property type is int, DateTime, GUID. then it is not working.
As we know Contains method only for array of elements or for string. I am thinking the value to property should type cast to string. So How to do it? Solution with Explanation is help full.
i Collected code from this.
public static Expression<Func<T, bool>> ContainsExp<T>(string propertyName, string contains)
{
var parameterExp = Expression.Parameter(typeof(T), "type");
var propertyExp = Expression.Property(parameterExp, propertyName);
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var someValue = Expression.Constant(contains, typeof(string));
var containsMethodExp = Expression.Call(propertyExp, method, someValue);
return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
}
Well, you probably know that's it's not possible to use ToString() in linq to entities.
So the following question is : how can I convert other types to string.
For numeric values, you have SqlFunctions.StringConvert, but it has only overloads for double? and decimal?
For DateTime, you may find something using SqlFunctions.StringConvert after having applied SqlFunctions.DatePart on your DateTime (which probably means at least 3 call to SqlFunctions.DatePart, for year, month, day)
For Guid, I don't think there's a way to do it directly. One way (at db level, if you use Sql Server) could be to have a Computed column. The computed column could store a varchar converted representation of your GUID. Maybe there's a better way.
Anyway, here's at least a sample which should work for integer as well as string:
public static Expression<Func<T, bool>> ContainsExp<T>(string propertyName, string contains)
{
//first, get the type of your property
var propertyType = typeof(T).GetProperty(propertyName).PropertyType;
//no change
var parameterExp = Expression.Parameter(typeof (T), "type");
Expression propertyExp = Expression.Property(parameterExp, propertyName);
//if property's type is int
if (propertyType == typeof (int))
{
//convert your Expression to a nullable double (or nullable decimal),
//so that you can use SqlFunctions.StringConvert
propertyExp = Expression.Convert(propertyExp, typeof (double?));
//get the SqlFunctions.StringConvert method for nullable double
var stringConvertMethod = typeof (SqlFunctions).GetMethod("StringConvert", new[] {typeof (double?)});
//call StringConvert on your converted expression
propertyExp = Expression.Call(stringConvertMethod , propertyExp);
}
//no change
var method = typeof (string).GetMethod("Contains", new[] {typeof (string)});
var someValue = Expression.Constant(contains, typeof (string));
var containsMethodExp = Expression.Call(propertyExp, method, someValue);
return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
}
public static IQueryable<T> FieldsContains<T>(this IQueryable<T> query, List<string> fileds, string searchValue)
{
Expression predicate = null;
var parameterExpression = Expression.Parameter(typeof(T), "type");
foreach (string field in fileds)
{
var next = GetFieldContainsExpression<T>(parameterExpression, field, searchValue);
if (predicate == null)
{
predicate = next;
}
else
{
predicate = Expression.Or(predicate, next);
}
}
var lambda = Expression.Lambda<Func<T, bool>>(predicate, parameterExpression);
return query.Where(lambda);
}
private static Expression GetFieldContainsExpression<T>(ParameterExpression parameterExpression, string field, string value)
{
var propertyType = typeof(T).GetProperty(field).PropertyType;
Expression propertyExpression = Expression.Property(parameterExpression, field);
var filterValue = Expression.Constant(value);
var method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
//call toString first to ignore type errors(datetime, int ...)
var toStringExpression = Expression.Call(propertyExpression, "ToString", Type.EmptyTypes);
var containsExpression = Expression.Call(toStringExpression, method, filterValue);
return containsExpression;
}
I'm basically trying to construct a query, and I don't know why microsoft made this so difficult in Entity Framework and LINQ. I have various parameter STRINGS. So if you see a variable, assume it's a string passed in from somewhere.
users = this.entities.tableUsers
.Where(searchfield+" LIKE %#0%", search)
.OrderBy(x => x.GetType().GetProperty(order_by).GetValue(x, null).ToString())
.Skip(Convert.ToInt32(limit_begin))
.Take(Convert.ToInt32(limit_end))
.ToList();
My question is what to put inside "Where()" function in LINQ.
I want to search a field with string "searchfield", for the value .contains() "search".
Not sure why Visual Studio won't let me do this easily.
I've tried this as well, no luck:
.Where(x => x.GetType().GetProperty(searchfield).GetValue(x, null).ToList().Contains(search))
Note: I don't want to install any new libraries, this should be incredibly easy and simple for a modern language. I don't mind if the query returns all the rows and I search through it AFTER with .Contains().
This is not trivial, but I believe it can be done. The following has not been tested. The code is borrowed from here.
Create a helper method somewhere like
public static Expression<Func<T, bool>> GetContainsExpression<T>(string propertyName, string containsValue)
{
var parameterExp = Expression.Parameter(typeof(T), "type");
var propertyExp = Expression.Property(parameterExp, propertyName);
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var someValue = Expression.Constant(propertyValue, typeof(string));
var containsMethodExp = Expression.Call(propertyExp, method, someValue);
return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
}
public static Expression<Func<T, TKey>> GetPropertyExpression<T, TKey>(string propertyName)
{
var parameterExp = Expression.Parameter(typeof(T), "type");
var exp = Expression.Property(parameterExp, propertyName);
return Expression.Lambda<Func<T, TKey>>(exp, parameterExp);
}
Use it like
users = this.entities.tableUsers
.Where(GetContainsExpression<User>(searchfield, search))
.OrderBy(GetPropertyExpression<User, string>(searchfield))
...
UPDATE
As an alternative, you could create extension methods to provide a cleaner syntax. Create the following methods in a static class somewhere:
public static IQueryable<T> WhereStringContains<T>(this IQueryable<T> query, string propertyName, string contains)
{
var parameter = Expression.Parameter(typeof(T), "type");
var propertyExpression = Expression.Property(parameter, propertyName);
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var someValue = Expression.Constant(contains, typeof(string));
var containsExpression = Expression.Call(propertyExpression, method, someValue);
return query.Where(Expression.Lambda<Func<T, bool>>(containsExpression, parameter));
}
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> query, string propertyName)
{
var propertyType = typeof(T).GetProperty(propertyName).PropertyType;
var parameter = Expression.Parameter(typeof(T), "type");
var propertyExpression = Expression.Property(parameter, propertyName);
var lambda = Expression.Lambda(propertyExpression, new[] { parameter });
return typeof(Queryable).GetMethods()
.Where(m => m.Name == "OrderBy" && m.GetParameters().Length == 2)
.Single()
.MakeGenericMethod(new[] { typeof(T), propertyType })
.Invoke(null, new object[] { query, lambda }) as IOrderedQueryable<T>;
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> query, string propertyName)
{
var propertyType = typeof(T).GetProperty(propertyName).PropertyType;
var parameter = Expression.Parameter(typeof(T), "type");
var propertyExpression = Expression.Property(parameter, propertyName);
var lambda = Expression.Lambda(propertyExpression, new[] { parameter });
return typeof(Queryable).GetMethods()
.Where(m => m.Name == "OrderByDescending" && m.GetParameters().Length == 2)
.Single()
.MakeGenericMethod(new[] { typeof(T), propertyType })
.Invoke(null, new object[] { query, lambda }) as IOrderedQueryable<T>;
}
Then you can call them like:
var users = this.entities.tableUsers.WhereStringContains(searchField, search)
.OrderBy(searchField);
this should be incredibly easy and simple for a modern language
No, it should not if it goes against that language paradigm. LINQ and Entity Framework (as well as any other decent ORM out there) are made precisely to avoid what you're trying to accomplish: non-typed and non-compiler-verifiable queries. So basically you're forcing square peg into round hole.
You can still take a look at Dynamic LINQ.
You'll have to build an expression tree to pass to the Where method. Here's a loose adaptation of some code I have lying about:
string searchfield, value; // Your inputs
var param = Expression.Parameter(typeof(User), "user");
return Expression.Lambda<Func<T, bool>>(
Expression.Call(
Expression.Property(
param,
typeof(User).GetProperty(searchfield)),
typeof(string).GetMethod("Contains"),
Expression.Constant(value)),
param);
That will generate an appropriate expression to use as the parameter to Where.
EDIT: FYI, the resultant expression will look something like user => user.Foo.Contains(bar).
EDIT: To sort, something like this (ripped from my DynamicOrderList class):
private IQueryable<T> OrderQuery<T>(IQueryable<T> query, OrderParameter orderBy)
{
string orderMethodName = orderBy.Direction == SortDirection.Ascending ? "OrderBy" : "OrderByDescending";
Type t = typeof(T);
var param = Expression.Parameter(t, "user");
var property = t.GetProperty(orderBy.Attribute);
return query.Provider.CreateQuery<T>(
Expression.Call(
typeof(Queryable),
orderMethodName,
new Type[] { t, typeof(string) },
query.Expression,
Expression.Quote(
Expression.Lambda(
Expression.Property(param, property),
param))
));
}
My answer to your other question about this:
When creating dynamic linq sorting and searching order statements in Entity Framework