I have the following method:
public List<Customer> SearchTest(string city, int skip, int take)
{
EcomContext db = new EcomContext();
var results = db.Customers.Where(n => n.City == city).OrdeyBy(n => n.Name).Skip(skip).Take(10);
results = AddDeleteCheck<Customer>(results);
return results.ToList()
}
And this reusable method:
private IQueryable<T> AddArchivedCheck<T>(IQueryable<T> data)
{
var parameter = Expression.Parameter(typeof(T));
var e1 = Expression.Equal(Expression.Property(parameter, "Archived"), Expression.Constant(false));
var e2 = data.Expression;
var e3 = Expression.Lambda<Func<T, bool>>(Expression.AndAlso(e1, e2), parameter);
return data.Where(e3);
}
I want to be able to call this method from a number of different functions so i have made it generic. It should take the expression from the IQueryable object and add a check onto this (Archived == false).
I am getting this error:
The binary operator AndAlso is not defined for the types 'System.Boolean' and 'System.Linq.IQueryable` [Ecom.Customer]
It is easier than what you wrote:
private static IQueryable<T> AddArchivedCheck<T>(IQueryable<T> data)
{
var parameter = Expression.Parameter(typeof(T));
var e1 = Expression.Equal(Expression.Property(parameter, "Archived"), Expression.Constant(false));
var lambda = Expression.Lambda<Func<T, bool>>(e1, parameter);
return data.Where(lambda);
}
Remember that in Linq:
var result = query.Where(condition1).Where(condition2);
is equivalent to:
var result = query.Where(condition1 && condition2);
Related
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);
}
}
I'm working with an IQueryable<SomeRandomObject> that is pulled using an EF Core 3.1 data context.
I'm pretty sure I can dynamically build a predicate for .Where() so that I can pass a string in for what column, and what value.
Of course this doesn't work, but some pseudo-code might be:
IQueryable myQueryable = stuffFromContext;
var columnName = "memberid";
var searchValue = "1234";
var results = myQueryable.Where(x=> someMagicColumnFunction(columnName, searchvalue))
I've only done research at this point, and predicate building is not my area of expertise.
Can someone help me create a function that I can pass in the parameters my IQueryable, a string representing the column name, and a string for the search (full equality for now, no 'like').
I'd love to see how this is done. I can't find a solid example anywhere on how to do something small like this. Most of the examples are everything and the kitchen sink!
Assuming the type of the rows in myQueryable are TQueryable then you can create a myQueryable specific function to generate the lambda:
Expression<Func<TQueryable, bool>> EqualsFilter<TCol>(string columnName, TCol searchValue) {
// build x => x.{columnName} == searchValue
// (TQueryable x)
var xParam = Expression.Parameter(typeof(TQueryable), "x");
// x.{columnName}
var colExpr = Expression.Property(xParam, columnName);
// {searchValue}
var constExpr = Expression.Constant(searchValue);
// x.{columnName} == {searchValue}
var lambdaBody = Expression.MakeBinary(ExpressionType.Equal, colExpr, constExpr);
// (TQueryable x) => x.{columnName} == {searchValue}
var lambda = Expression.Lambda<Func<TQueryable, bool>>(lambdaBody, xParam);
return lambda;
}
Once you have the method, you can use it like:
var myQueryable = stuffFromContext;
var columnName = "memberid";
var searchValue = "1234";
var results = myQueryable.Where(EqualsFilter(columnName, searchvalue));
However, if myQueryable has a complex or anonymous type (because of a Select or Join) you need to replace the Where as C# can only infer types from parameters, so you need the myQueryable parameter to get the entity type you are filtering. Using a generic version of EqualsFilter as a helper method, you have:
public static class IQueryableExt {
static Expression<Func<T, bool>> EqualsFilter<T, TCol>(string columnName, TCol searchValue) {
// build x => x.{columnName} == searchValue
// (T x)
var xParam = Expression.Parameter(typeof(Accounts), "x");
// x.{columnName}
var colExpr = Expression.Property(xParam, columnName);
// {searchValue}
var constExpr = Expression.Constant(searchValue);
// x.{columnName} == {searchValue}
var lambdaBody = Expression.MakeBinary(ExpressionType.Equal, colExpr, constExpr);
// (T x) => x.{columnName} == {searchValue}
var lambda = Expression.Lambda<Func<T, bool>>(lambdaBody, xParam);
return lambda;
}
public static IQueryable<T> WhereColumnEquals<T, TCol>(this IQueryable<T> src, string columnName, TCol searchValue)
=> src.Where(EqualsFilter<T, TCol>(columnName, searchValue));
}
Which you can now use like:
var myQueryable = stuffFromContext;
var columnName = "memberid";
var searchValue = "1234";
var results = myQueryable.WhereColumnEquals(columnName, searchvalue);
Goodafternoon everyone!
I'm trying to translate request in SQL:
SELECT *
FROM TABLE1
WHERE TABLE1.ID IN (SELECT ID FROM TABLE2)
into LINQ to OData.
As far as "IN" is not supported by ODAta protocol,WHERE part must be like TABLE1.ID=1 OR TABLE1.ID=2 OR ...,
I've tried to code generic method, that takes as input list of id's and returns correct Expression for LINQ in that way:
public static Expression<Func<T,bool>> Lambda<T>(this Expression expr ,List<int> ids)
{
ParameterExpression argParam = Expression.Parameter(typeof(T), "rep");
Expression<Func<T, bool>> lambda = code => 1 == 0;
var lambdaPred = Expression.Lambda<Func<T, bool>>(lambda.Body, argParam);
var attr = (DataServiceKeyAttribute)typeof(T).GetCustomAttribute(typeof(DataServiceKeyAttribute));
string keyName;
try
{
keyName = attr.KeyNames.FirstOrDefault();//get name of key attribute
}
catch
{
return null;
}
foreach (int id in ids)
{
var property = typeof(T).GetProperty(keyName);
Expression<Func<T, bool>> lambdatemp = code => (int)property.GetValue(code) == id;
var tmp = Expression<Func<T, bool>>.Or(lambdaPred.Body, lambdatemp.Body);
lambdaPred = Expression.Lambda<Func<T, bool>>(tmp, argParam);
}
return lambdaPred;
}
usage of this method:
Expression<Func<Client, bool>> lambda = code => 1 == 0;
var query = lambda.Body.Lambda<Client>(ids);
var retr = clientRepository.Retrieve(query).ToList();
but at runtime I'm getting error:
An exception of type 'System.NotSupportedException' occurred in
Microsoft.Data.Services.Client.dll but was not handled in user code
Additional information: The expression (((((False Or (Convert(Int32
Id.GetValue(code)) == 1044)) Or (Convert(Int32 Id.GetValue(code)) ==
8102)) Or (Convert(Int32 Id.GetValue(code)) == 5997)) Or
(Convert(Int32 Id.GetValue(code)) == 7761)) Or (Convert(Int32
Id.GetValue(code)) == 15455)) is not supported.
Do you know any ways to fix this problem?
The generic method you wrote is quite unclear (both signature and implementation). As I understand, the idea is to build expression like this
x => x.Id == Id1 || x.Id == Id2 || ....
Here is one possible way of such a method by using Expression.Equal and Expression.OrElse
public static class PredicateHelper
{
public static Expression<Func<T, bool>> In<T>(this Expression<Func<T, int>> idSelector, IEnumerable<int> ids)
{
Expression body = null;
foreach (var id in ids)
{
var operand = Expression.Equal(idSelector.Body, Expression.Constant(id));
body = body == null ? operand : Expression.OrElse(body, operand);
}
return body != null ? Expression.Lambda<Func<T, bool>>(body, idSelector.Parameters) : null;
}
}
Sample usage equivalent to your example
var idFilter = PredicateHelper.In((Client c) => c.Id, ids);
var result = clientRepository.Retrieve(idFilter).ToList();
I have the following function:
public List<TopMediansModel> QueryForMedians(string state, string median)
{
ApplicationDbContext db = new ApplicationDbContext();
return (from q in db.StateSuburbLocation
where (q.State == state)
orderby q.GetType().GetProperty(median) descending
select new TopMediansModel
{
Median = median,
MedianValue = q.GetType().GetProperty(median),
MedianSuburb = q.Suburb
}).Take(10).ToList();
}
Is it possible to have the orderby and MedianValue 'properties' as variables?
I've tried using the GetType().GetProperty() Methods, and am no doubt doing so incorrectly.
You can use something like Dynamic Linq
return db.StateSuburbLocation
.Where(l => l.State == state)
.OrderByDescending(median)
.Select(l => new TopMediansModel {
Median = median,
MedianValue = q.GetType().GetProperty(median),
MedianSuburb = q.Suburb
}).Take(10).ToList();
Or you can build query expression manually
public static IQueryable<T> OrderByDescending<T>(
this IQueryable<T> source, string propertyName)
{
var parameter = Expression.Parameter(typeof(T), "p");
var property = Expression.PropertyOrField(parameter, propertyName);
var keySelector = Expression.Lambda<Func<T, object>>(property, parameter);
return source.OrderByDescending(keySelector);
}
Usage is same
I try to make my custom orderby extension method, i successfully worked my code but in addition i want to list null or empty or zero values last in result, anyone can help me about that issue ?
Here is my extension method to orderby
public static IQueryable<T> OrderBy<T>(this IQueryable<T> q, string SortField, bool isAsc)
{
//var nullExpr = Expression.Constant(null, typeof(T));
var param = Expression.Parameter(typeof(T), "p");
var prop = Expression.Property(param, SortField);
var exp = Expression.Lambda(prop, param);
string method = isAsc ? "OrderBy" : "OrderByDescending";
Type[] types = new Type[] { q.ElementType, exp.Body.Type };
var mce = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
return q.Provider.CreateQuery<T>(mce);
}
Thanks in advance
The simplest way is to use
OrderBy(e => String.IsNullOrEmpty(e.TeamName)
This doesn't require any extension method or custom IComparer implementation etc.
var entries = repository.Race.Where(e => e.EventId == id)
.OrderBy(e => String.IsNullOrEmpty(e.TeamName))
.ThenBy(e => e.LastName)
.ThenBy(e => e.FirstName);
Without using an extension method....
Create a custom IComparer<string> to check the empty values before using the default String.Compare. The first checks will return -1 instead of 1 or 1 instead of -1, if using the standard string comparison.
/// <summary>
/// Returns -1 instead of 1 if y is IsNullOrEmpty when x is Not.
/// </summary>
public class EmptyStringsAreLast : IComparer<string>
{
public int Compare(string x, string y)
{
if (String.IsNullOrEmpty(y) && !String.IsNullOrEmpty(x))
{
return -1;
}
else if (!String.IsNullOrEmpty(y) && String.IsNullOrEmpty(x))
{
return 1;
}
else
{
return String.Compare(x, y);
}
}
}
Pass your EmptyStringsAreLast comparer into the OrderBy of Lambda expression. In this solution teams who have entered the race should appear alphabetical order, but the unaffiliated race entries should appear at then end.
var entries = repository.Race.Where(e => e.EventId == id)
.OrderBy(e => e.TeamName, new EmptyStringsAreLast())
.ThenBy(e => e.LastName)
.ThenBy(e => e.FirstName);
This answer is perhaps what you were originally looking for - using your generic extension method:
public static IQueryable<T> OrderByFieldNullsLast<T>(this IQueryable<T> q, string SortField, bool Ascending)
{
//We are rebuilding .OrderByDescending(p => p.SortField.HasValue).ThenBy(p => p.SortField)
//i.e. sort first by whether sortfield has a value, then by sortfield asc or sortfield desc
//create the expression tree that represents the generic parameter to the predicate
var param = Expression.Parameter(typeof(T), "p");
//create an expression tree that represents the expression p=>p.SortField.HasValue
var prop = Expression.Property(param, SortField);
var hasValue = Expression.Property(prop, "HasValue");
var exp = Expression.Lambda(hasValue, param);
string method = "OrderByDescending";
Type[] types = new Type[] { q.ElementType, exp.Body.Type };
var orderByCallExpression = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
//now do the ThenBy bit,sending in the above expression to the Expression.Call
exp = Expression.Lambda(prop, param);
types = new Type[] { q.ElementType, exp.Body.Type };
method = Ascending ? "ThenBy" : "ThenByDescending";
var ThenByCallExpression = Expression.Call(typeof(Queryable), method, types,orderByCallExpression, exp);
return q.Provider.CreateQuery<T>(ThenByCallExpression);
}
Building on Dave Anson's answer, you can user Comparer.Create() to create the Comparer from a lambda. Here's an example that sorts unsorted by its myString string fields, with null or empty strings appearing last.
var sorted = unsorted.OrderBy(x => x.myString, Comparer<string>.Create((x, y) => {
if ( string.IsNullOrEmpty(y) && !string.IsNullOrEmpty(x)) return -1;
else if (!string.IsNullOrEmpty(y) && string.IsNullOrEmpty(x)) return +1;
else return string.Compare(x, y);
}))
(To put them first, switch the signs on the 1 constants)
it works for me:
private static IQueryable<T> GetOrderQuery<T>(this IQueryable<T> q, BaseFilterCollection filter)
{
q = q.OrderBy(GetExpression<T>(filter.SortField));
var param = Expression.Parameter(typeof(T), "p");
var prop = Expression.Property(param, filter.SortField);
var exp = Expression.Lambda(prop, param);
string method = filter.SortDirection == SortDirectionType.Asc ? "ThenBy" : "ThenByDescending";
Type[] types = { q.ElementType, exp.Body.Type };
var rs = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
return q.Provider.CreateQuery<T>(rs);
}
private static Expression<Func<T, bool>> GetExpression<T>(string sortField)
{
ParameterExpression param = Expression.Parameter(typeof(T), "p");
Expression prop = Expression.Property(param, sortField);
var info = typeof(T).GetProperty(sortField, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
Expression exp = Expression.Equal(prop, info.PropertyType.IsValueType
? Expression.Constant(Activator.CreateInstance(info.PropertyType))
: Expression.Constant(null));
return Expression.Lambda<Func<T, bool>>(exp, param);
}
You dont need to complicate, the easiest way is to do something like this:
YourList.OrderByDescending(x => string.IsNullOrEmpty(x.value)
Use OrderByDescending or OrderBy depending on if you want to see empty strings in the beginning or last.
Regards