在我的数据存储库中,我有一个基类和一个派生类,如下所示。
当我点击“投射”功能时,出现以下错误:
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);
}
}
当我点击“投射”功能时,出现以下错误:
我该如何转换此表达式值?在类型“System.Func`2 [CommentUrlCommon,System.Boolean]”和“System.Linq.Expressions.Expression`1 [System.Func`2 [CommentUrl,System.Boolean]]”之间未定义强制转换运算符。
FindOneAsync(int primaryKey)
)。 - Kirill Shlenskiy