I have tried searching but I don't seem to find any relevant answers. Perhaps because I'm not really sure how to formulate my question.
I'm writing a class library to aid working with SharePoint's Client Side Object Model. When executing a query, one can specify which properties of returned objects should be loaded, in order to avoid unnecessary network traffic. This is done by the means of Lambda Expression.
Here is an example that works:
public ListItemCollection GetItems(
params Expression<Func<ListItemCollection, object>>[] retrievals)
{
var query = new CamlQuery {...};
ListItemCollection queryResults = _list.GetItems(query);
ReloadClientObject(queryResults, retrievals)
return queryResults;
}
public void ReloadClientObject<T>(T clientObject,
params Expression<Func<T, object>>[] retrievals)
where T : ClientObject
{
_context.Load(clientObject, retrievals);
_context.ExecuteQuery();
}
Example call:
var items = GetItems(items => items.Include(
item => item.Id,
item => item.DisplayName));
This would all be fine. But I'd rather return IEnumerable<ListItem> instead of ListItemCollection and I would like to pass parameters of type Expression<Func<ListItem, object>> instead of Expression<Func<ListItemCollection, object>>... not to introduce the user to the ListItemCollection at all. So I'd like to move the Include() call to the body of my method... and that's where I got stuck.
Here's what I've got so far:
public IEnumerable<ListItem> GetItems(
params Expression<Func<ListItem, object>>[] retrievals)
{
var query = new CamlQuery {...};
ListItemCollection queryResults = _list.GetItems(query);
ReloadClientObject(queryResults, items => items.Include(retrievals))
_context.ExecuteQuery();
return queryResults.AsEnumerable();
}
Example call (much cleaner and nicer):
var items = GetItems(item => item.Id, item => item.DisplayName));
However, this throws OperationNotSupportedException when calling the Load() method.
I would be grateful for any guidance. Thank you!
Call Include directly on the query itself, and then just use LoadQuery instead of Load, to load the query:
public IEnumerable<ListItem> GetItems(this ClientContext context,
string listName,
params Expression<Func<ListItem, object>>[] retrievals)
{
var query = new CamlQuery();
var queryResults = context.Web.Lists.GetByTitle(listName)
.GetItems(query)
.Include(retrievals);
context.LoadQuery(queryResults);
context.ExecuteQuery();
return queryResults;
}
Since that doesn't work for you (according to your comment stating that you need to leverage the paging functionality) we'll need to do a bit more work.
So what we'll do here is create a Expression<Func<ListItemCollection, ItemSelector, object>> that will take a collection, a selector, and map that to an object. Here ItemSelector is defined through using ItemSelector = Expression<Func<ListItem, object>>; (Because trying to use a Expression<Func<ListItemCollection, Expression<Func<ListItem, object>>, object>> is just cruel and unusual punishment). We can define it like so:
Expression<Func<ListItemCollection, ItemSelector, object>> includeSelector =
(items, selector) => items.Include(selector);
Now what we can do is write an Apply method that can take an expression of a function taking two parameters, replace all instances of the second parameter with the constant, and thus create a method with one less parameter. Here is the definition of that Apply method:
public static Expression<Func<T1, TResult>> Apply<T1, T2, TResult>(
this Expression<Func<T1, T2, TResult>> expression,
T2 value)
{
return Expression.Lambda<Func<T1, TResult>>(
expression.Body.Replace(expression.Parameters[1],
Expression.Constant(value))
, expression.Parameters[0]);
}
This uses this helper method to replace all instances of one expression with another:
public static Expression Replace(this Expression expression,
Expression searchEx, Expression replaceEx)
{
return new ReplaceVisitor(searchEx, replaceEx).Visit(expression);
}
internal class ReplaceVisitor : ExpressionVisitor
{
private readonly Expression from, to;
public ReplaceVisitor(Expression from, Expression to)
{
this.from = from;
this.to = to;
}
public override Expression Visit(Expression node)
{
return node == from ? to : base.Visit(node);
}
}
So now we can take this includeSelector expression and, for each item selector in our array, apply that selector to this function. Taking those results and putting them into an array gives us an Expression<Func<ListItemCollection, object>>[], which is exactly what we need to pass to Load.
Whew. Here is the final code to actually do that:
public static IEnumerable<ListItem> GetItems(this ClientContext context,
string listName,
params Expression<Func<ListItem, object>>[] retrievals)
{
var query = new CamlQuery();
var queryResults = context.Web.Lists.GetByTitle(listName)
.GetItems(query);
Expression<Func<ListItemCollection, ItemSelector, object>> includeSelector =
(items, selector) => items.Include(selector);
context.Load(queryResults, retrievals
.Select(selector => includeSelector.Apply(selector))
.ToArray());
context.ExecuteQuery();
return queryResults;
}
Related
My Extension Method is as follows:
public static IQueryable<TSource> TrialBatch<TSource>(this IQueryable<TSource> sourceQuery, List<long> Ids, Expression<Func<TSource, object>> expression)
{
// Expected Code
}
expression variable receiving in the extension method will be as follows "x => x.EmployeeID"
Is it possible to convert the expression as follows?
"x => Ids.Contains(x.EmployeeID)" so that we could combine it with the 'sourceQuery' and return the same.
This is similar to dbContext.EmployeeIDDetails.Where(x => Ids.Contains(x.EmployeeID)).ToList();
The only difference is that we will be sending the Ids and where condition("x => x.EmployeeID") dynamically with respective to tables.
I am using this type of extension method for a development purpose and also I'm curious if this is feasible. Kindly add a comment if you have any queries
Thanks in advance.
Use LINQKit to allow the selector to be expanded within another expression:
public static IQueryable<TSource> WhereIn<TSource, TProp>(
this IQueryable<TSource> query,
IEnumerable<TProp> list,
Expression<Func<TSource, TProp>> selector)
{
return query.AsExpandable()
.Where(item => list.Contains(selector.Invoke(item)));
}
If you don't want to use LinqKit, you can write your own method to compose expressions together.
The Compose method is as simple as replacing all instances of the parameter of the composing method with the body of the composed method:
public static Expression<Func<TFirstParam, TResult>>
Compose<TFirstParam, TIntermediate, TResult>(
this Expression<Func<TFirstParam, TIntermediate>> first,
Expression<Func<TIntermediate, TResult>> second)
{
var param = Expression.Parameter(typeof(TFirstParam), "param");
var newFirst = first.Body.Replace(first.Parameters[0], param);
var newSecond = second.Body.Replace(second.Parameters[0], newFirst);
return Expression.Lambda<Func<TFirstParam, TResult>>(newSecond, param);
}
This uses the following method to replace all instances of an expression with another:
public static Expression Replace(this Expression expression,
Expression searchEx, Expression replaceEx)
{
return new ReplaceVisitor(searchEx, replaceEx).Visit(expression);
}
internal class ReplaceVisitor : ExpressionVisitor
{
private readonly Expression from, to;
public ReplaceVisitor(Expression from, Expression to)
{
this.from = from;
this.to = to;
}
public override Expression Visit(Expression node)
{
return node == from ? to : base.Visit(node);
}
}
And now you can write:
public static IQueryable<TSource> WhereIn<TSource, TProp>(
this IQueryable<TSource> query,
IEnumerable<TProp> sequence,
Expression<Func<TSource, TProp>> selector)
{
return query.Where(selector.Compose(value => sequence.Contains(value)));
}
so I want to make filtering work automaticly based on some easy settings. The code I have is this:
public ActionResult Index() // here I want to add filtering for Status I only want to show the active ones
{
IQueryable<Ticket> cases = db.Cases().AsQueryable();
cases = cases.EnablePaging().EnableFilterFor(x => x.Status);
return View(cases);
}
EnableFilterFor looks like this:
public static IQueryable<T> EnableFilterFor<T>(this IQueryable<T> queryable, Expression<Func<T, string>> keySelector)
{
string filterValue= "Active";
//Expression<Func<T, bool>> whereexpresion = keySelector.Compile() == "Active"
queryable = queryable.Where(
//here do the magic !! so that the result will be 'x=>x.Status == filterValue');
);
return queryable;
}
I googled a lot, tried many different things but no success. I somehow have to combine the keySelector and the filterValue to work (I need an Expression for the Where method to work). Any help would be greatly appreciated.
EDIT: After testing both solutions (thank you both!) I found out that Poke had the best one. Poke his code is the only code that doesn't change the way that the SQL is generated. When I took a look at Servy his generated SQL it always did an EXTRA Sql select query and an EXTRA and in the WHERE clause... No idea why :)
IQueryable.Where requires an Expression<Func<T, bool>>, so that will be the thing we need to build. As we want to integrate something from another expression (a Expression<Func<T, string>>), we have to build the expression “by hand”.
So in the end, we want to call LambdaExpression.Lambda<Func<T, bool>>(…) to get our expression for Where, but we need to fill in the expression body:
// first, we reuse the parameter from the `keySelector` expression
ParameterExpression param = keySelector.Parameters[0];
// The body is now just an equality comparison of the `keySelector`
// body, and the constant `filterValue`
Expression body = Expression.Equal(keySelector.Body, Expression.Constant(filterValue));
// now we just need to create a lambda expression for that body with the
// saved parameter and it’s all done:
queryable = queryable.Where(Expression.Lambda<Func<T, bool>>(body, param));
What we'll need here is a Compose method, for expressions. It'll take an expression that uses a value, and another expression that conceptually will use the result of the first expression as its input, generating a new output.
public static Expression<Func<TFirstParam, TResult>>
Compose<TFirstParam, TIntermediate, TResult>(
this Expression<Func<TFirstParam, TIntermediate>> first,
Expression<Func<TIntermediate, TResult>> second)
{
var param = Expression.Parameter(typeof(TFirstParam), "param");
var newFirst = first.Body.Replace(first.Parameters[0], param);
var newSecond = second.Body.Replace(second.Parameters[0], newFirst);
return Expression.Lambda<Func<TFirstParam, TResult>>(newSecond, param);
}
It will require the ability to replace one expression with another, which we can do using the following:
public static Expression Replace(this Expression expression,
Expression searchEx, Expression replaceEx)
{
return new ReplaceVisitor(searchEx, replaceEx).Visit(expression);
}
internal class ReplaceVisitor : ExpressionVisitor
{
private readonly Expression from, to;
public ReplaceVisitor(Expression from, Expression to)
{
this.from = from;
this.to = to;
}
public override Expression Visit(Expression node)
{
return node == from ? to : base.Visit(node);
}
}
Now we can write:
public static IQueryable<T> EnableFilterFor<T>(
this IQueryable<T> queryable,
Expression<Func<T, string>> keySelector)
{
string filterValue= "Active";
return queryable.Where(keySelector.Compose(status => status == filterValue));
}
I have a generic repository in which I'm trying to include a function that accepts a variable list of child tables to eagerly load. The function looks thus:
public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includeEntities)
{
IQueryable<T> query = this._dbSet.Where(e => !e.Deleted).Where(predicate);
foreach (var entity in includeEntities)
{
query.Include(entity);
}
return query;
}
It works but I'm concerned about the object reference.
Using the function thus:
var foundEntities = Repository.Entities.FindBy(i => i.Id == targetId, i => i.Orders, i => i.Invoices);
The params passed in the includeEntites array are of type System.Linq.Expressions.PropertyExpression which is unfortunately an internal class so I can't make the function signature:
public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate, params Expression<Func<T, System.Linq.Expressions.PropertyExpression>>[] includeEntities)
as I'd like. Any thoughts?
I have an expression like this
(a,b) => a.Id == b.Id
I would like to use it in LINQ to Entities query
T GetSingle(IRepository<T> repository, Func<T,T,bool> predicate, T entity)
{
return repository.GetAll().Single(e => predicate(e, entity))
}
but this results the exception: LINQ expression node type 'Invoke' is not supported in LINQ to Entities
As I understand I can use Expressions to construct a valide predicate for LINQ2SQL, so my expression
(a,b) => a.Id == b.Id and instance of entity with Id = 5 can result a new expression (a) => a.Id == 5.
And the last expression will be fine for LINQ to Entities.
I found and read this articles
Replace parameter in lambda expression
http://www.codeproject.com/Articles/143096/Parameter-Substitution-within-Expression-Trees
but still has no clue how to solve my task
So, how do I convert given expression dynamically?
Why don't you just change your method to be:
T GetSingle(IRepository<T> repository, Expression<Func<TSource, Boolean>> predicate)
{
return repository.GetAll().Single(predicate);
}
so instead of this:
GetSingle(myRepository, (a,b) => a.Id == b.Id, myEntity);
you should be able to do this:
GetSingle(myRepository, a => a.Id == myEntity.Id);
I haven't tested it with Linq2SQL, but it seems to me that you should be able to do this with an expression visitor and compiling the expression to write the value of your parameter into the expression you've supplied (assuming you switch over to using Expression<Func<T, T, bool>> instead of Func<T, T, bool>) and creating a wrapper that itself invokes Enumerable.Single on the result from the GetAll
The visitor (for specifically the example you've given would look like this)
public class VariableSubstitutionVisitor : ExpressionVisitor
{
private readonly ParameterExpression _parameter;
private readonly ConstantExpression _constant;
public VariableSubstitutionVisitor(ParameterExpression parameter, ConstantExpression constant)
{
_parameter = parameter;
_constant = constant;
}
protected override Expression VisitParameter(ParameterExpression node)
{
if (node == _parameter)
{
return _constant;
}
return node;
}
}
Now, we'd adjust the GetSingle method to look like this:
public T GetSingle(IRepository<T> repository, Expression<Func<T, T, bool>> predicate, T entity)
{
//Create a new representation of predicate that will take just one parameter and capture entity
//Get just the body of the supplied expression
var body = predicate.Body;
//Make a new visitor to replace the second parameter with the supplied value
var substitutionVisitor = new VariableSubstitutionVisitor(predicate.Parameters[1], Expression.Constant(entity, typeof(T)));
//Create an expression that represents the predicate with the second parameter replaced with the supplied entity
var visitedBody = substitutionVisitor.Visit(body).Reduce();
//Make the new expression into something that could be a Func<T, bool>
var newBody = Expression.Lambda<Func<T, bool>>(visitedBody, predicate.Parameters[0]);
//Now, create something that will call Enumerable.Single on the result of GetAll from the repository, supplying the new predicate
//Make a place to hold the result of GetAll
var resultsParameter = Expression.Parameter(typeof (IEnumerable<T>));
//Make an expression that calls the Single extension method
var singleExpression = Expression.Call(((Func<IEnumerable<T>, Func<T, bool>, T>)Enumerable.Single).Method, resultsParameter, newBody);
//Make a Func<IEnumerable<T>, T> that return the result of the call of Single on the results of the GetAll method
var compiled = Expression.Lambda<Func<IEnumerable<T>, T>>(singleExpression, resultsParameter).Compile();
//Call GetAll, letting the new method that we've got run the supplied predicate without having to run an Invoke type expression
return compiled(repository.GetAll());
}
The trick, of course, is getting that to perform well.
I'm trying to build a generic method that EF4.1 to look in both the Database and the Local memory for a particular row in a table that matches a particular criteria.
So far, this is what I have this.
This is the caller.
dbEntities.MyTables.LocalAndDb(delegate(MyTable s)
{ return s.Description.Contains("test"); });
This is LocalAndDb
public static object LocalAndDb<T>(this DbSet<T> myTable, Func<T, bool> function) where T : class
{
// look in local
var item = myTable.Local.Where(o => function((T)o)).FirstOrDefault()
// if not exist, look in the database
if (item == null)
{
Expression<Func<T, bool>> predicate = (u) => function(u);
item = myTable.Where(predicate).FirstOrDefault();
}
return item;
}
The problem is with this line.
item = myTable.Where(predicate).FirstOrDefault();
When it calls the database, it throws this error.
"The LINQ expression node type 'Invoke' is not supported in LINQ to Entities."
I imagine it's because I'm passing in an anonymous method and it doesn't know how to turn this into SQL. I thought converting it to an Expression object would do the trick but it's still not working for me.
What do I need to do to make a anonymous method become something that LINQ can turn into SQL?
To make this work, you need to pass the lambda expression to LocalAndDb as an expression tree (so that LINQ to Entities can analyze the code and translate it to SQL):
public static object LocalAndDb<T>(this DbSet<T> myTable,
Expression<Func<T, bool>> expr) where T : class {
// ...
if (item == null) {
item = myTable.Where(expr).FirstOrDefault();
}
return item;
}
Then, of course, the problem is that you cannot execute the expression tree when checking the in-memory data. One way to solve this is to use the Compile method of Expression<T>, but that will be a bit inefficient (depending on your scenario).
Another option is to just pass the condition as both function and expression tree:
public static object LocalAndDb<T>(this DbSet<T> myTable,
Func<T, boo> function, Expression<Func<T, bool>> expr) where T : class {
var item = myTable.Local.Where(o => function((T)o)).FirstOrDefault();
if (item == null) {
item = myTable.Where(expr).FirstOrDefault();
}
return item;
}
table.LocalAndDb(t => t.Foo > 10, t => t.Foo > 10);
This is a bit ugly, but it doesn't require inefficient compilation at runtime. If you want a slightly more sophisticated solution, then you can define your own type to keep pre-compiled functions:
class Precompiled<T1, T2> {
public Precompiled(Expression<Func<T1, T2>> expr) {
this.Expression = expr;
this.Function = expr.Compile();
}
public Expression<Func<T1,T2>> Expression { get; private set; }
public Func<T1,T2> Function { get; private set; }
}