Converting Expression<Func<DTO,bool>> to Expression<Func<Domain, bool>> - c#

I'm using Expression Tree Serializer for sending Expression< FuncDTO,bool>> to WCF services and
my repository is working with Domain type. I want to apply this expression for my repository
Expression<Func<UserDto,bool>> expression = new ExpressionSerializer().Deserialize<Func<UserDto, bool>>(xmlElement);
var addressBookEntries = addressBooksRepository.Where(expression); //accepts Expression<Func<UserDomain,bool>>
How can I fix this problem?

I used dynamic linq and it solved my problem, only little issue property names must be equals. I converted dynamic linq to string and sent it thorough wcf service and using it as string
using System.Linq;
using System.Linq.Dynamic;
public ICollection<UserDto> GetUsersByFilter(string filter)
{
var addressBooksRepository = new AddressBooksRepository();
var addressBookEntries = addressBooksRepository.GetAll().Where(filter);
//return data
}

Try create new expression with your type arguments based on existed one.
public class UserDto : UserDomain
{ }
public class UserDomain
{ }
void YourMethod()
{
Expression<Func<UserDto, bool>> expression = new ExpressionSerializer().Deserialize<Func<UserDto, bool>>(xmlElement);
Func<UserDto, bool> func = expression.Compile();
Expression<Func<UserDomain, bool>> newExpression = x => func(x as UserDto);
var addressBookEntries = addressBooksRepository.Where(newExpression);
}

Related

Create New Expression With Different Lambda Signature From Existing Expression Body

Is it possible to do what I'm looking for here:
namespace ExpressionProblem
{
class Program
{
private static readonly List<dynamic> Items = new List<dynamic>
{
new { Name = "Foo" },
new { Name = "Bar" }
};
static void Main()
{
var result = DoSomething<Item>(p => p.Name == "Foo");
Console.WriteLine(result.Name);
}
static T DoSomething<T>(Expression<Func<T, bool>> expression)
{
//change expression lambda from Func<T, bool> to Func<dynamic, bool> so below will compile and work
return Items.FirstOrDefault(expression);
}
}
public class Item
{
public string Name { get; set; }
}
}
Basically I need to take the given expression with has a signature of
Expression<Func<T, bool>>
and get and expression which performs the same action but has the signature
Expression<Func<dynamic, bool>>.
Is this even possible? From what I've read I don't think you can change an existing expression as most places I've researched have said you basically have to build a new expression from an existing one. I tried to do the following:
var newExpression = Expression.Lambda<Func<dynamic, bool>>(expression.Body);
...but am getting the error "Incorrect number of parameters supplied for lambda declaration" when trying to create the new expression.
Any ideas on how to get this to work or am I trying to do something that can't (or shouldn't) be done?
Thanks in advance for any help you can provide.
Regards,
Craig
[EDIT] - I know some will ask why I don't just make the list of type Item - in my particular case I am not able to as the list to be queried will be in an app domain that has no knowledge of the Item type. I'm just including it in the same namespace/class here for brevity's sake.[/EDIT]

Combining AndAlso The parameter 'foo' was not bound in the specified LINQ to Entities query expression

I have an entity.
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
}
I want to create my own expression predicate. For that I have created a method that accepts property name and the value.
private static Expression<Func<Foo, bool>> Condition(string pName, object value)
{
var pe = Expression.Parameter(typeof(Foo), "foo");
var left = Expression.Property(pe, pName);
var right = Expression.Constant(value);
var equal = Expression.Equal(left, right);
var predicate = Expression.Lambda<Func<Foo, bool>>(equal, pe);
return predicate;
}
This is the predicate which works fine for a single condition.
using (var db = new MyEntities())
{
var predicate = Condition("Name", "foo");
var foos = db.Foos.Where(predicate).ToArray();
}
But when I tried to combine two conditions by following this post, it throws exception.
The parameter 'foo' was not bound in the specified LINQ to Entities
query expression.
using (var db = new MyEntities())
{
var cond1 = Condition("Name", "foo");
var cond2 = Condition("Code", "bar");
var body = Expression.AndAlso(cond1.Body, cond2.Body);
var predicate = Expression.Lambda<Func<Foo,bool>>(body, cond1.Parameters[0]);
var foos = db.Foos.Where(predicate).ToArray(); // exception
}
Please enlighten me.
The problem is that ParameterExpression in LINQ expressions is identified by reference equality, but the two Parameter objects are different references. (The name in ParameterExpression only exists for debugging purposes).
(If you reread the mentioned post, it says that the method that you tried would only work if both lambdas are defined on the same ParameterExpression object).
You have two big possibilities at this stage: either you define a way for the Condition function to accept a ParameterExpression object, or you create an ExpressionVisitor that will replace the original ParameterExpression with another. (Of course, given that you want to do an AndAlso, you could also conceivably chain two Where clauses, but that is less general.)

Selecting distinct entity values based on string field name

Is it possible to select distinct property values of an entity where the column name is not known in advance - received by the method as a string from the client.
For filtering purposes I want to make a HTTP POST Ajax request to the server from the browser which will contain the field name as a key, and this will be available to the server as a string.
I know I am going to have to manually map any differences between the ViewModel and the POCO class used by Entity Framework, but is it possible to construct an Entity framework query without the strongly typed property - by using reflection for example.
I'd probably try to implement this where the data was determined by controller, calling a generic method of the base repository with the entity class as the type. Is this possible or do I need to construct a method for each possible field?
Equally, should I be attempting to do this, or instead constructing ADO.NET SQL commands with the field as a parameter (mapping from ViewModel to SQL column names)?
I'm pretty sure I saw a similar answer recently but I can't find it...
If you know the type of your entity and the type of the property you're going to be selecting, this is pretty easy:
public IQueryable<TProperty> SelectProperty<TEntity, TProperty>(DbContext context, string propertyName)
where TEntity : class
{
var parameter = Expression.Parameter(typeof(TEntity));
var body = Expression.Property(parameter, propertyName);
var lambda = Expression.Lambda<Func<TEntity, TProperty>>(body, parameter);
var result = context.Set<TEntity>().Select (lambda).Distinct();
return result;
}
If you can't predict the type of the property then building the expression will be more difficult:
public IQueryable SelectProperty<TEntity>(DbContext context, string propertyName)
where TEntity : class
{
var entities = context.Set<TEntity>();
var query = entities.AsQueryable();
var parameter = Expression.Parameter(typeof(TEntity), "instance");
var propertyAccess = Expression.Property(parameter, propertyName);
var projection = Expression.Lambda(propertyAccess, parameter);
var selectExpression = Expression.Call(
typeof(Queryable).GetMethods()
.First (x => x.Name == "Select")
.MakeGenericMethod(new[]{ typeof(TEntity), propertyAccess.Type }),
query.Expression,
projection);
var distinctExpression = Expression.Call(
typeof(Queryable).GetMethods()
.First (x => x.Name == "Distinct")
.MakeGenericMethod(new[]{ propertyAccess.Type }),
selectExpression);
var result = query.Provider.CreateQuery(distinctExpression);
return result;
}
You can use Dyanmic Linq (nuget package: System.Linq.Dynamic) to do this, along with an extension method which I will not even try to take credit for, found here
Sample code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace dlinq1
{
class Thing
{
public int ID { get; set; }
}
class Program
{
static void Main(string[] args)
{
var things = new List<Thing>();
for(int x=0;x<10;x++)
{
things.Add(new Thing{ID=x%2}); //0,1,0,1,0,1,0,1,0,1
}
var result = things.AsQueryable().Select("ID").Distinct();
foreach (var r in result)
{
Console.WriteLine(r);
}
Console.ReadLine(); //produces 0, 1
}
}
public static class DynamicQueryableExtras
{
public static IQueryable Distinct(this IQueryable q)
{
var call = Expression.Call(typeof(Queryable),
"Distinct",
new Type[] { q.ElementType },
q.Expression);
return q.Provider.CreateQuery(call);
}
}
}
Basically the extension works with whatever state the queryable is in since the previous expression just before you called Distinct.

Create an Expression<Func<,>> using reflection

Im using Moq to create mocks of a data set.
I have created a little helper class that allows me to have an in memory storage instead of a database that makes unit testing a breeze. That way I can add and remove items from my mock data set, this allows me to test my insert and delete service calls.
During the setup of the mock I have a line that looks like the following
this.Setup(i => i.AcademicCycles).Returns(mockStore.GetList<AcademicCycle>());
My mock has a lot of properties so I would like to perform this setup step using reflection. I have managed to the Returns part of the process working via reflection but I am stuck on the lambda method to Setup.
Setup takes an
Expression<Func<GoalsModelUnitOfWork, IQueryable<AcademicCycle>>> that corresponds to the i => i.AcademicCycles
and I would like to create this dynamically. Using reflection I have the following:
The name of the property: "AcademicCycles"
The type IQueryable<AcademicCycle>
The type AcademicCycle
I also have the instance of the i in the lambda statement which is a GoalsModelUnitOfWork
The code to create the expression dynamically would be like this:
ParameterExpression parameter = Expression.Parameter(typeof (GoalsModelUnitOfWork), "i");
MemberExpression property = Expression.Property(parameter, "AcademicCycles");
var queryableType = typeof (IQueryable<>).MakeGenericType(typeof (AcademicCycle));
var delegateType = typeof (Func<,>).MakeGenericType(typeof (GoalsModelUnitOfWork), queryableType);
var yourExpression = Expression.Lambda(delegateType, property, parameter);
The result will have the desired type, but the problem is that the return type of Expression.Lambda() is LambdaExpression and you can't perform a type cast to Expression<Func<...>> to pass it as parameter to your setup function because you don't know the generic type parameters for the Func. So you have to invoke the Setup method by reflection, too:
this.GetType().GetMethod("Setup", yourExpression.GetType()).Invoke(this, yourExpression);
I decided to take a crack at it and ended up with this god awful piece of code.
I am no reflection expert and this is just a first attempt to get something working. I'd be very interested in what other approaches people have, or whether any of the relfection wrapper libraries can make this nicer.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Moq;
using Xunit;
namespace MyExample
{
public class Tests
{
[Fact]
public void Test()
{
Dictionary<Type, object> data = new Dictionary<Type, object>
{
{ typeof(IQueryable<Cycle>), new List<Cycle> { new Cycle { Name = "Test" } }.AsQueryable() },
{ typeof(IQueryable<Rider>), new List<Rider> { new Rider { Name = "1"}, new Rider { Name = "2" } }.AsQueryable() }
};
var mock = new Mock<IDataContext>();
var setup = mock.GetType().GetMethods().Single(d => d.Name == "Setup" && d.ContainsGenericParameters);
var param = Expression.Parameter(typeof(IDataContext), "i");
foreach (var property in typeof(IDataContext).GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
// Build lambda
var ex = Expression.Lambda(Expression.MakeMemberAccess(param, property), param);
// Get generic version of the Setup method
var typedSetup = setup.MakeGenericMethod(property.PropertyType);
// Run the Setup method
var returnedSetup = typedSetup.Invoke(mock, new[] { ex });
// Get generic version of IReturns interface
var iReturns = typedSetup.ReturnType.GetInterfaces().Single(d => d.Name.StartsWith("IReturns`"));
// Get the generic Returns method
var returns = iReturns.GetMethod("Returns", new Type[] { property.PropertyType });
// Run the returns method passing in our data
returns.Invoke(returnedSetup, new[] { data[property.PropertyType] });
}
Assert.Equal(1, mock.Object.Cycles.Count());
}
}
public class Cycle
{
public string Name { get; set; }
}
public class Rider
{
public string Name { get; set; }
}
public interface IDataContext
{
IQueryable<Cycle> Cycles { get; set; }
IQueryable<Rider> Riders { get; set; }
}
}
This method ought to construct the lambda expression. Since you are invoking the Setup method by reflection, you do not need a strongly-typed lambda expression; you are going to pass it as part of an object array when you call Invoke:
public LambdaExpression PropertyGetLambda(string parameterName, Type parameterType, string propertyName, Type propertyType)
{
var parameter = Expression.Parameter(parameterType, parameterName);
var memberExpression = Expression.Property(parameter, propertyName);
var lambdaExpression = Expression.Lambda(memberExpression, parameter);
return lambdaExpression;
}
I don't think you actually need the parameter name. If I'm right about that, you could simplify a bit:
public LambdaExpression PropertyGetLambda(Type parameterType, string propertyName, Type propertyType)
{
var parameter = Expression.Parameter(parameterType);
var memberExpression = Expression.Property(parameter, propertyName);
var lambdaExpression = Expression.Lambda(memberExpression, parameter);
return lambdaExpression;
}

Create New Expression from Existing Expression

I have an Expression<Func<T,DateTime>> I want to take the DateTime part of the expression and pull the Month off of it. So I would be turning it into a Expression<Func<T,int>> I'm not really sure how to do this. I looked at the ExpressionTree Visitor but I can't get it to work like I need. Here is an example of the DateTime Expression
DateTimeExpression http://img442.imageshack.us/img442/6545/datetimeexpression.png
Here is an example of what I want to create
MonthExpression http://img203.imageshack.us/img203/8013/datetimemonthexpression.png
It looks like I need to create a new MemberExpression that is made up of the Month property from the DateTime expression but I'm not sure.
Yes, that's exactly what you want - and using Expression.Property is the easiest way to do that:
Expression func = Expression.Property(existingFunc.Body, "Month");
Expression<Func<T, int>> lambda =
Expression.Lambda<Func<T, int>>(func, existingFunc.Parameters);
I believe that should be okay. It works in this simple test:
using System;
using System.Linq.Expressions;
class Person
{
public DateTime Birthday { get; set; }
}
class Test
{
static void Main()
{
Person jon = new Person
{
Birthday = new DateTime(1976, 6, 19)
};
Expression<Func<Person,DateTime>> dateTimeExtract = p => p.Birthday;
var monthExtract = ExtractMonth(dateTimeExtract);
var compiled = monthExtract.Compile();
Console.WriteLine(compiled(jon));
}
static Expression<Func<T,int>> ExtractMonth<T>
(Expression<Func<T,DateTime>> existingFunc)
{
Expression func = Expression.Property(existingFunc.Body, "Month");
Expression<Func<T, int>> lambda =
Expression.Lambda<Func<T, int>>(func, existingFunc.Parameters);
return lambda;
}
}

Categories

Resources