My problem derived from complex reusable logical specifications.
I have the following Expression
:
Expression<Func<User, bool>> userExp =
user => user.UserInRoles.Any(userInRole => userInRole.RoleId == SystemRoles.SysAdmin.Id);
And I need to get the ability of:
new CommonContext().Set<Estate>().Where(estate => userExp.WithParameter(estate.CreatorUser)).ToList();
So how can I pass the Creator
of Estate
entity into the expression which accepts a User
entity and finally use the final expression in linq to sql
?
The problem is : WithParameter
EDIT:
This one works but its not efficient:
new CommonContext().Set<Estate>().ToList().Where(estate => userExp.Compile()(estate.CreatorUser)).ToList()
And the following is not the answer because the Invoke method
can not be translated to store expression:
Expression<Func<User, bool>> userExp =
user => user.UserInRoles.Any(userInRole => userInRole.RoleId == SystemRoles.SysAdmin.Id);
Expression<Func<Estate, User>> propAccessor = estate => estate.ApprovedByUser;
var estateParam = Expression.Parameter(typeof(Estate));
var userParam = Expression.Invoke(propAccessor, estateParam);
var translatedExp = Expression.Invoke(userExp, userParam);
var result = (Expression<Func<Estate, bool>>)Expression.Lambda(translatedExp, estateParam);
var exceptionProvider = new CommonContext().Set<Estate>().Where(result).ToList();
But I need something which can be translated into Store Expression
maybe the final solution is decomposing and then recomposing the expression , and if so ,, how can I encapsulate it for reusing it in similar situations? (as this is what i'm trying to do)