I am building Expression tree for Linq-to-sql
. In database on some of tables relevant columns are stored as string
while some are stored as Guid
. I have solved similar problem with int
and int?
by wrapping lambda constant with Expression.Convert(Expression.Constant(search.PolicyNumber), policyNumberColumnLambda.Type)
(where PolicyNumber
sometimes was nullable
) which worked just fine. But it does not fly for Guid
to string
conversion apparently.
Code looks following:
public static IQueryable<IRetrieveGuid> SearchByRetrieveGuid<IRetrieveGuid>(this IQueryable<IRetrieveGuid> queryable, SearchModel search)
{
var paramLambda = Expression.Parameter(typeof(IRetrieveGuid));
var columnLambda = Expression.Property(paramLambda, "retrieveguid");
var lambda = Expression.Lambda<Func<IRetrieveGuid, bool>>(
Expression.Equal(columnLambda, Expression.Convert(Expression.Constant(search.RetrieveGuid), columnLambda.Type)), paramLambda);
return queryable.Where(lambda);
}
How do I convert types to match in expression tree?