How to query collection with Linq.Expressions - linq

I built a custom IQueryable provider. The provider transforms query for example
c.PurchaseDate == new DateTime(2011, 11, 29) && c.Name == "Elizabeth Brown"
from underlying code into System.Linq.Expressions.Expression
Now I need to run them against this collection with Linq query
IQueryable<Customer> customers = _customers.AsQueryable();
Can anyone tell me how to query the collection with Expression?
Thanks

//Query = c.PurchaseDate == new DateTime(2011, 11, 29) && c.Name
// == "Elizabeth Brown" )
IQueryable<Customer> customers = _customers.AsQueryable<Customer>();
//Predicate parameter
ParameterExpression parameter = Expression.Parameter(typeof(Customer),
"customer");
//Create left expression
Expression left = Expression.Property(parameter, typeof(Customer)
.GetProperty("PurchaseDate"));
Expression right = Expression.Constant(new DateTime(2011, 11, 29));
Expression leftExp = Expression.Equal(left, right);
//Create right expression tree
left = Expression.Property(parameter, typeof(Customer).GetProperty("Name"));
right = Expression.Constant("Elizabeth Brown", typeof(string));
Expression rightExp = Expression.Equal(left, right);
//Combine the expressions into expression tree
Expression expressionTree = Expression.AndAlso(leftExp, rightExp);
//Create an expression tree that represents the expression
MethodCallExpression methodCall = Expression.Call(
typeof(Queryable),
"Where",
new Type[] { customers.ElementType },
customers.Expression,
Expression
.Lambda<Func<Customer, bool>>
(expressionTree, new ParameterExpression[] { parameter }));
// Create an executable query from the expression tree.
IQueryable<Customer> results =
customers.Provider.CreateQuery<Customer>(methodCall);
// Enumerate the results
foreach (Customer customer in results)
{
Console.WriteLine("{0} {1}", customer.Name, customer.PurchaseDate);
}
Console.ReadLine();
I finished with the task this way. IQueryable is really wonderfull stuff. Enjoy!

Related

EF Core : expression trees - GroupJoin transform to SelectMany dynamically

I would like to transform the below query:
(from documentType in entitySet
join userGroupId in Repository.ConvertToBigIntTable(userGroupIds, "userGroupId")
on documentType.Id equals userGroupId.Id into UserGroupIds
from userGroupId in UserGroupIds.DefaultIfEmpty()
join documentTypePermission in Repository.DocumentTypePermissions
on documentType.Id equals documentTypePermission.DocumentTypeId
join userSelectionParam in Repository.UserSelectionParams
on documentTypePermission.UserSelectionId equals userSelectionParam.Id
where documentTypePermission.IsActive &&
((userSelectionParam.UserGroupId != null && userGroupId.Id != null)
|| (userSelectionParam.UserId != null && userSelectionParam.UserId == CurrentUserId))
select documentTypePermission);
to something like this:
var query =
from documentType in entitySet
from userGroupId in Repository.ConvertToBigIntTable(userGroupIds, "userGroupId")
.Where(userGroupId => documentType.Id == userGroupId.Id)
.DefaultIfEmpty()
join documentTypePermission in Repository.DocumentTypePermissions
on documentType.Id equals documentTypePermission.DocumentTypeId
join userSelectionParam in Repository.UserSelectionParams
on documentTypePermission.UserSelectionId equals userSelectionParam.Id
....
Note:
I have the setup ready for intercepting the expression tree evaluation using QueryTranslationPreprocessor
I need the logic to generate the output of .Call, .SelectMany etc by using the first linq and then translating the same to second linq query shown above
The caveats are that internally the expression engine generates so many Anonymous types that I am unable to write a generic code that would suffice different cases of linq with such groupjoins
Reason to do this:
There are several kinds of GroupJoin issues in EF Core 6 which the community isn’t ready to resolve
I can't make the change to Linq query directly as there are 1000s of them and in several places
So after evaluating multiple options, the best way to do the above is as below at VisitMethodCall(MethodCallExpression node)
if (node.Method.DeclaringType == typeof(Queryable) && node.Method.Name == nameof(Queryable.GroupJoin) && node.Arguments.Count == 5)
{
var outer = Visit(node.Arguments[0]);
var inner = Visit(node.Arguments[1]);
var outerKeySelector = Visit(node.Arguments[2]).UnwrapLambdaFromQuote();
var innerKeySelector = Visit(node.Arguments[3]).UnwrapLambdaFromQuote();
var resultSelector = Visit(node.Arguments[4]).UnwrapLambdaFromQuote();
var outerKey = outerKeySelector.Body.ReplaceParameter(outerKeySelector.Parameters[0], resultSelector.Parameters[0]);
var innerKey = innerKeySelector.Body;
var keyMatch = MatchKeys(outerKey, innerKey);
var innerQuery = Expression.Call(
typeof(Queryable), nameof(Queryable.Where), new[] { innerKeySelector.Parameters[0].Type },
inner, Expression.Lambda(keyMatch, innerKeySelector.Parameters));
var asEnumerableInnerQuery = Expression.Call(
typeof(Enumerable),
nameof(Enumerable.AsEnumerable),
new Type[] { innerKeySelector.Parameters[0].Type }, innerQuery);
var resultTypes = resultSelector.Parameters.Select(p => p.Type).ToArray();
var tempProjectionType = typeof(Tuple<,>).MakeGenericType(resultTypes);
var tempProjection = Expression.New(
tempProjectionType.GetConstructor(resultTypes),
new Expression[] { resultSelector.Parameters[0], asEnumerableInnerQuery },
tempProjectionType.GetProperty("Item1"), tempProjectionType.GetProperty("Item2"));
var tempQuery = Expression.Call(
typeof(Queryable), nameof(Queryable.Select), new[] { outerKeySelector.Parameters[0].Type, tempProjectionType },
outer, Expression.Lambda(tempProjection, resultSelector.Parameters[0]));
var tempResult = Expression.Parameter(tempProjectionType, "p");
var projection = resultSelector.Body
.ReplaceParameter(resultSelector.Parameters[0], Expression.Property(tempResult, "Item1"))
.ReplaceParameter(resultSelector.Parameters[1], Expression.Property(tempResult, "Item2"));
var query = Expression.Call(
typeof(Queryable), nameof(Queryable.Select), new[] { tempProjectionType, projection.Type },
tempQuery, Expression.Lambda(projection, tempResult));
return query;
}
return base.VisitMethodCall(node);
}

IQueryable.Union/Concat in .net core 3

I want to add a dummy member to an IQueryable and came up with this solution:
IQueryable<Geography> geographies = _unitOfWork.GeographyRepository.GetAll(); //DbSet<Geography>
var dummyGeographies = new Geography[] { new Geography { Id = -1, Name = "All" } }.AsQueryable();
var combinedGeographies = geographies.Union(dummyGeographies);
var test = combinedGeographies.ToList(); //throws runtime exc
But it throws the following exception:
Processing of the LINQ expression 'DbSet
.Union(EnumerableQuery { Geography, })' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core.
How could I make it work?!
you can only union on data structure which are the same
IQueryable is only applicable if the query expression not been been expressed (ToList) before its run against db and you want the expression modifiable . aka nothing which which is not going to db as a query needs to be IQueryable (simple explanation better to research and understand this yourself)
List<Geography> geographies = _unitOfWork.GeographyRepository
.GetAll() //DbSet<Geography>
.Select(o => new Geography { Id = o.Id, Name = o.Name })
.ToList();
List<Geography> dummyGeographies = new List<Geography>() {
new Geography[] { new Geography { Id = -1, Name = "All" } }
};
var combinedGeographies = geographies.Union(dummyGeographies);
var test = combinedGeographies.ToList();
I was able to achieve it with the following code:
IQueryable<Geography> geographies = _unitOfWork.GeographyRepository.GetAll().Select(o => new Geography { Id = o.Id, Name = o.Name });
IQueryable<Geography> dummyGeographies = _unitOfWork.GeographyRepository.GetAll().Select(o => new Geography { Id = -1, Name = "All" });
var combinedGeographies = geographies.Union(dummyGeographies);

Reflection within IQueryable in .NET Core

I am wondering how would the below code work and what would be the performance. I am interested in line #4 basically. How will linq and property info work together?
In other words will (int)prop.GetValue(a) and a.SomeId work the same way or will the reflection need to get everything to memory before checking the value?
var prop = type.GetProperty("SomeId");
if (prop != null)
{
DbSet<T> dbSet = _dbContext.Set<T>();
return dbSet.Where(a => (int)prop.GetValue(a) == 1);
}
In other words will (int)prop.GetValue(a) and a.SomeId work the same way
No.
Depending on the backing store, the context may fail to convert that expression ((int)prop.GetValue(a)) to the underlying query syntax, which in most cases would be SQL.
You could consider building a valid expression manually using the property info.
For example
//Assuming
Type type = typeof(T);
PropertyInfo prop = type.GetProperty("SomeId");
if (prop != null) {
//need to build a => a.SomeId == 1
// a =>
ParameterExpression parameter = Expression.Parameter(type, "a");
// a => a.SomeId
MemberExpression property = Expression.Property(parameter, prop);
// a => a.SomeId == 1
BinaryExpression body = Expression.Equal(property, Expression.Constant(1));
Expression<Func<T, bool>> expression = Expression.Lambda<Func<T, bool>>(body, parameter);
DbSet<T> dbSet = _dbContext.Set<T>();
return dbSet.Where(expression);
}

Determine the return type of dynamic type

I have chosen to create an anonymous type for temporary projection in a LINQ Join query. I am using ExpressionTrees, to build the query at runtime. I donot know if the following code would help me with creating a temporary projected sequence.
Here is the code that does the temporary Projection:
private Expression<Func<EntityObject, EntityObject,dynamic>> TempProjectionExpression
{
get
{
return (o, p) => new
{
o = o,
p = p
};
}
}
My Join Query using Expression Trees is given below.
public IQueryable<dynamic> GetQueryExpressionresults3<T, U, V>(IQueryable<T> aEntityCollection1, IQueryable<U> aEntityCollection2, Type[] _TypeArguments ,V _anonymousType, string aPropertyName)
where T : EntityObject
where U : EntityObject
{
ParameterExpression pe = Expression.Parameter(typeof(U), "o");
ParameterExpression pe1 = Expression.Parameter(typeof(T), "p");
//This should be populated from UI
Expression me = Expression.Property(pe1, typeof(T).GetProperty(aPropertyName));
//This should be populated from UI
Expression me1 = Expression.Property(pe, typeof(U).GetProperty(aPropertyName));
LambdaExpression le = Expression.Lambda<Func<T, int>>(me, new ParameterExpression[] { pe1 });
LambdaExpression le1 = Expression.Lambda<Func<U, int>>(me1, new ParameterExpression[] { pe });
_TypeArguments = new Type[] { aEntityCollection1 .ElementType, aEntityCollection2.ElementType, le.Body.Type, typeof(MovieCollections)};
//_TypeArguments = _TypeArguments.Concat(new Type[] { le.Body.Type, typeof(object) }).ToArray();
MethodCallExpression JoinCallExpression = Expression.Call(typeof(Queryable), "Join", _TypeArguments, aEntityCollection1.Expression, aEntityCollection2.Expression
, le, le1, TempProjectionExpression);
var oResult = aEntityCollection1.Provider.CreateQuery(JoinCallExpression) as IQueryable<dynamic>;
return oResult;
}
Now the question is, i would like to determine the return type of the TempProjectionExpression, i.e. typeof(dynamic). Is this possible? If yes, then how?
typeof(dynamic) can't do any better than System.Object (which the compiler won't even try to do) which isn't a very interesting result.
You can use returnedResult.GetType(), though, to get its runtime type.
Since dynamic puts off type resolution to runtime, there's no way to tell what the return is before something is actually returned without doing type analysis on your expression tree worthy of the C# compiler itself.

How to implement outer join expression tree?

I need to implement the query using expressions syntax (because I don't know types in compile time). For example query like this one:
from customer in Customers
join purchase in Purchases
on customer.ID equals purchase.CustomerID
into outerJoin
from range in outerJoin.DefaultIfEmpty()
where
customer.Name == "SomeName" &&
range.Description.Contains("SomeString") &&
customer.ID == range.CustomerID
select
new { Customer = customer, Purchase = range }
I found way to implement group join part like this:
ITable p = _dataContext.GetTable(typeof(Purchases));
ITable c = _dataContext.GetTable(typeof(Customers));
LambdaExpression outerSelectorLambda = DynamicExpression.ParseLambda(typeof(Customers), null, "ID");
LambdaExpression innerSelectorLambda = DynamicExpression.ParseLambda(typeof(Purchases), null, "CustomerID");
ParameterExpression param1 = Expression.Parameter(typeof(Customers), "customer");
ParameterExpression param2 = Expression.Parameter(typeof(IEnumerable<Purchases>), "purchases");
ParameterExpression[] parameters = new ParameterExpression[] { param1, param2 };
LambdaExpression resultsSelectorLambda = DynamicExpression.ParseLambda(parameters, null, "New(customers as customers, purchases as purchases)");
MethodCallExpression joinCall =
Expression.Call(
typeof(Queryable),
"GroupJoin",
new Type[] {
typeof(Customers),
typeof(Purchases),
outerSelectorLambda.Body.Type,
resultsSelectorLambda.Body.Type
},
c.Expression,
p.Expression,
Expression.Quote(outerSelectorLambda),
Expression.Quote(innerSelectorLambda),
Expression.Quote(resultsSelectorLambda)
);
But I can't figure out how to write rest of query using this syntax.
Does anyone can help me?
I would follow an approach to achieve that:
Get the Expression equivalent of the LINQ query.
Get the ToString() of the Expression extracted from the LINQ query.
Study the Expression, to understand the input parameters, type parameters, Expression Arguments etc...
Get back to me if the approach mentioned is not clear.
I just copied + pasted the "join" implementation in dynamic.cs and made couple of changes to make "GroupJoin" to work.
public static IQueryable GroupJoin(this IQueryable outer, IEnumerable inner, string outerSelector, string innerSelector, string resultsSelector, params object[] values)
{
if (inner == null) throw new ArgumentNullException("inner");
if (outerSelector == null) throw new ArgumentNullException("outerSelector");
if (innerSelector == null) throw new ArgumentNullException("innerSelector");
if (resultsSelector == null) throw new ArgumentNullException("resultsSelctor");
LambdaExpression outerSelectorLambda = DynamicExpression.ParseLambda(outer.ElementType, null, outerSelector, values);
LambdaExpression innerSelectorLambda = DynamicExpression.ParseLambda(inner.AsQueryable().ElementType, null, innerSelector, values);
Type resultType = typeof(IEnumerable<>).MakeGenericType(inner.AsQueryable().ElementType);
ParameterExpression[] parameters = new ParameterExpression[]
{
Expression.Parameter(outer.ElementType, "outer"), Expression.Parameter(resultType, "inner")
};
LambdaExpression resultsSelectorLambda = DynamicExpression.ParseLambda(parameters, null, resultsSelector, values);
return outer.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "GroupJoin",
new Type[] { outer.ElementType, inner.AsQueryable().ElementType, outerSelectorLambda.Body.Type, resultsSelectorLambda.Body.Type },
outer.Expression, inner.AsQueryable().Expression, Expression.Quote(outerSelectorLambda), Expression.Quote(innerSelectorLambda), Expression.Quote(resultsSelectorLambda)));
}

Resources