I would like to create repository pattern class which gets items by query. Unfortunately I need to parse this query from one class to another (Picture to ListItem) to send it to a server(api). So my code should looks like below:
public static void ConvertQuery(Expression<Func<Picture, object>> oldQuery)
{
Expression<Func<ListItem, object>> newQuery = convert(oldQuery);
}
And, for example, i want to convert old query by cast properties like below:
- SomePicture.Id => SomeListItem.Id
- SomePicture.FileName => SomeListItem["FileName"]
I found some solutions where I can cast properties. But the biggest problem is with casting one property to dictionary field (item1.Filename to item2.["Filename"])
Update
@nejcs
I've tried to use you solution but unfortunately I have exception:
System.ArgumentException: 'ParameterExpression of type 'Microsoft.SharePoint.Client.ListItem' cannot be used for delegate parameter of type 'CastExpression.Picture''
The property "Item" is responsible for dictionary values, however I think that there is a problem with conversion. Below is stackTrace:
at System.Linq.Expressions.Expression.ValidateLambdaArgs(Type delegateType, Expression& body, ReadOnlyCollection 1 parameters) at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, String name, Boolean tailCall, IEnumerable 1 parameters) at System.Linq.Expressions.Expression 1.Update(Expression body, IEnumerable`1 parameters) at System.Linq.Expressions.ExpressionVisitor.VisitLambda[T](Expression 1 node) at System.Linq.Expressions.Expression 1.Accept(ExpressionVisitor visitor) at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at CastExpression.Program.Main(String[] args)
I also know how Expression body looks like
For oldClassQuery:
Expression<Func<Picture, object>> oldQuery = x => x.FileName == "AS";
{x => Convert((x.FileName == "AS"))}
For newClassQuery:
Expression<Func<ListItem, object>> newQuery = x => x["FileName"] == "AS";
{x => Convert((x.get_Item("FileName") == "AS"))}
ExpressionVisitorand rewrite the expression tree manually. - Andrey Nasonov