In my Data Repository I have a base class and derived class as below.
public abstract class RepositoryBase<T> : IRepository<T> where T : EntityBase
{
public async Task<T> FindOneAsync(Expression<Func<T, bool>> predicate)
{
List<T> list = await SearchForAsync(predicate);
return list.FirstOrDefault();
}
}
public class CommentUrlRepository : RepositoryBase<CommentUrl>, ICommentUrlRepository
{
public async Task<CommentUrlCommon> FindOneAsync(
Expression<Func<CommentUrlCommon, bool>> predicate
)
{
Expression<Func<CommentUrl, bool>> lambda = Cast(predicate);
CommentUrl commentUrl = await FindOneAsync(lambda);
return MappingManager.Map(commentUrl);
}
private Expression<Func<CommentUrl, bool>> Cast(
Expression<Func<CommentUrlCommon, bool>> predicate
)
{
Expression converted =
Expression.Convert(
predicate,
typeof(Expression<Func<CommentUrl, bool>>)
);
// throws exception
// No coercion operator is defined between types
return Expression.Lambda<Func<CommentUrl, bool>>(converted, predicate.Parameters);
}
}
When I hit "Cast" function I am getting following error:
No coercion operator is defined between types 'System.Func`2[CommentUrlCommon,System.Boolean]' and 'System.Linq.Expressions.Expression`1[System.Func`2[CommentUrl,System.Boolean]]'.
How can I cast this Expression value?
FindOneAsync(int primaryKey)
) instead. – Scalpel