Expression tree for groupby with where clause and than select - c#

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

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

How to get dynamic column data linq

I have following code,
public List<MemberDto> GetMembers(out int rowCount,int pageIndex,int pageSize, string seachColumn = "", string searchTerm = "", string sortBy = "", string sortDiection = "")
{
var members = (from m in context.Members
where (string.IsNullOrEmpty(searchTerm) || m.MemberNumber.Equals(searchTerm))
|| (string.IsNullOrEmpty(searchTerm) || m.LastName.Equals(searchTerm))
select m).AsEnumerable();
if (!string.IsNullOrEmpty(sortBy))
{
PropertyDescriptor prop = TypeDescriptor.GetProperties(typeof(EFModel.ClientData.Member)).Find(sortBy, true);
members = (sortDiection.ToLower() == "descnding") ? members.OrderByDescending(x => prop.GetValue(x)).ToList() : members.OrderBy(x => prop.GetValue(x)).ToList();
}
rowCount = (!string.IsNullOrEmpty(searchTerm)) ? members.Count() : rowCount = context.Members.Count() ;
members = members.Skip(pageIndex).Take(pageSize).ToList();
List<MemberDto> memberDtos = new List<MemberDto>();
mapper.Map(members, memberDtos);
return memberDtos;
}
In the above code, I seachColumn value can be ("a","b",or ""). When seachColumn = "a" I need to search table data by column MemberNumber based on searchTerm value
When seachColumn = "b" I need to search table data by column LastName. based on searchTerm value
to achieve that I wrote following code.
if(seachBy == "a")
{
var sa = (from m in context.Members
where (string.IsNullOrEmpty(searchTerm) || m.MemberNumber.Equals(searchTerm))
select m).AsEnumerable();
}
else if (seachBy == "b")
{
var sa = (from m in context.Members
where (string.IsNullOrEmpty(searchTerm) || m.LastName.Equals(searchTerm))
select m).AsEnumerable();
}
I know, I tried code is bit fool. Have any proper way to do this?
Yes there is a better way to do this. First of all you want to do this all as an IQueryable - Why? - Because as soon as you do .AsEnumerable() or .ToList() the query is executed on the DB Server and the data is loaded into Memory.
So in your code here - because you have called .AsEnumerable() it has loaded all context.Members.Where condition is true into Memory:
var members = (from m in context.Members
where (string.IsNullOrEmpty(searchTerm) || m.MemberNumber.Equals(searchTerm))
|| (string.IsNullOrEmpty(searchTerm) || m.LastName.Equals(searchTerm))
select m)
.AsEnumerable();
Your second part of code is pagination. What we normally do is write a IQueryable extension methods.
So add the following class with the 2 extension methods to a common location.
public static class IQueryableExtensions
{
public static IQueryable<T> ApplyPagination<T>(this IQueryable<T> source, string sortDirection, string sortBy, int pageNumber, int pageSize)
{
var sortDirectionInternal = sortDirection == "asc" ? "OrderBy" : "OrderByDescending";
var orderBy = sortBy;
if (pageSize != -1) // -1 is for All - I don't apply pagination if pageSize == -1.
{
return source.OrderBy(orderBy, sortDirectionInternal)
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize);
}
return source.OrderBy(orderBy, sortDirection);
}
public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string ordering, string sortDirection)
{
var type = typeof(T);
// Get Property to Sort By
var property = type.GetProperty(ordering);
// If Property is NULL (not found) - Just use the first Property (Default) to ORDER BY - as this will prevent Exception
if (property == null)
{
property = type.GetProperties().First();
}
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExp = Expression.Lambda(propertyAccess, parameter);
var resultExp = Expression.Call(typeof(Queryable), sortDirection, new[] { type, property.PropertyType }, source.Expression, Expression.Quote(orderByExp));
return source.Provider.CreateQuery<T>(resultExp);
}
}
So now your above code will look something like this - I didn't use VS so there might be some sytnax errors:
// NOTE: you will need to include the namespace for the new IQueryableExtensions class
var members = (from m in context.Members
where (string.IsNullOrEmpty(searchTerm) || m.MemberNumber.Equals(searchTerm))
|| (string.IsNullOrEmpty(searchTerm) || m.LastName.Equals(searchTerm))
select m);
// retrieve count only if you need to of total members that match the above criteria
rowCount = members.Count();
// This is all you need to do! - ApplyPagination(params) -
members = members.ApplyPagination(sortDiection, sortBy, pageIndex, pageSize);
return mapper.Map<List<MemberDto>>(members);

Generate dynamic LINQ expression based on array

I am using LINQ exprssion to query customers and filter them by state names. I have following query which works fine till the time I have 4 items in my statesArray.
public void GetCustomersForSelectedStates(string[] statesArray)
{
var customers = _repo.GetAllCustomers();
var filteredCustomers = from CUST in customers
join ST in States on CT.Tag_Id equals ST.Id
where CUST.ID == customer.ID && (ST.Name == statesArray[0] ||ST.Name ==statesArray[1] || ST.Name== statesArray[2]||ST.Name =statesArray[3])
//Do something with customers
}
I want following exprssion to be created dynamically:
(ST.Name == statesArray[0] ||ST.Name ==statesArray[1] ||
ST.Name== statesArray[2]||ST.Name =statesArray[3])
For example , create the dynamicQuery like below
var dynamicQuery = "(";
var dynamicQuery = "(";
for (int i = 0; i < statesArray.Count(); i++)
{
dynamicQuery += "ST.Name =="+statesArray[0];
if(i==statesArray.Count())
dynamicQuery+=")"
}
and then use it something like following,
//Psuedo code
var customers = _repo.GetAllCustomers();
var filteredCustomers = from CUST in customers
join ST in States on CT.Tag_Id equals ST.Id
where CUST.ID == customer.ID && Expression(dynamicQuery)
To do that via dynamic expressions basically means building a tree of:
(x.Foo == val0 || x.Foo == val1 || x.Foo == val2)
You can do that like this:
static Expression<Func<T, bool>> Where<T, TVal>(Expression<Func<T, TVal>> selector,
IEnumerable<TVal> values)
{
Expression result = null;
foreach (var val in values)
{
var match = Expression.Equal(
selector.Body,
Expression.Constant(val, typeof(TVal)));
result = result == null ? match : Expression.OrElse(result, match);
}
if (result == null) return x => true; // always match if no inputs
return Expression.Lambda<Func<T, bool>>(result, selector.Parameters);
}
with example usage:
string[] names = { "a", "c" };
var predicate = Where<Customer, string>(c => c.Name, names);
You can then use this predicate in the IQueryable<T>.Where extension method.
To combine it in your case, first do your regular LINQ:
var customers = _repo.GetAllCustomers();
var filteredCustomers = from CUST in customers
join ST in States on CT.Tag_Id equals ST.Id
where CUST.ID == customer.ID;
Now as a separate step apply the extra filter:
customers = customers.Where(predicate);
What this does is accept an input lambda of the form c => c.Name, then reuses the c.Name body for each c.Name == {val}, and reuses the c parameter as the parameter for the lambda we're creating. Each value from values becomes a typed constant. The Expression.Equal gives us the == test. The OrElse is the ||, noting that if result is null, this is the first item, so we just use the match expression itself.
You can add another join
var customers = _repo.GetAllCustomers();
var filteredCustomers = from CUST in customers
join ST in States on CT.Tag_Id equals ST.Id
join SA in statesArray on ST.Name equals SA // No need dynamic expression now
where CUST.ID == customer.ID
//Do something with customers

Operator "IN" by key field in Linq to OData

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

Using Func<> in Entity Framework Query

I have the following Entity Framework query:
Func<Company, bool> filter;
if (officeId != 0)
filter = company => !company.IsDeleted && company.OfficeCompanies.Any(c => c.OfficeId == officeId);
else
filter = company => !company.IsDeleted;
var companies = from c in Repository.Query<Company>()
where filter(c) &&
(relationshipTypes.Count() == 0 || relationshipTypes.Any(r => r == c.TypeEnumIndex)) &&
c.Description.Contains(term)
orderby c.Description
select new JqueryUiAutoCompleteItem
{
label = c.Description,
value = SqlFunctions.StringConvert((double)c.Id)
};
It gives me the error:
The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.
If I remove the reference to filter() in the main body of the query, there is no error.
I understand the meaning of this error: I am using code that cannot be converted into SQL. But what is there about my filter() that cannot be converted to SQL?
You need to switch the Func to an Expression, then pass that expression to a Where directly in LINQ fluent syntax. I don't think there's a way to use the expression in query syntax.
Expression<Func<Company, bool>> filter; //<-- changed type
if (officeId != 0)
filter = company => !company.IsDeleted && company.OfficeCompanies.Any(c => c.OfficeId == officeId);
else
filter = company => !company.IsDeleted;
var companies = from c in Repository.Query<Company>().Where(filter) // <-- changed syntax
where (relationshipTypes.Count() == 0 || relationshipTypes.Any(r => r == c.TypeEnumIndex)) &&
c.Description.Contains(term)
orderby c.Description
select new JqueryUiAutoCompleteItem
{
label = c.Description,
value = SqlFunctions.StringConvert((double)c.Id)
};

Categories

Resources