表达式类型转换错误 - 在这些类型之间没有定义强制转换运算符

12
在我的数据存储库中,我有一个基类和一个派生类,如下所示。
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
1个回答

3
我认为你所需的无法做到... 更多信息请查看这个问题。 如果你很幸运,你的表达式很简单,马克·格拉维尔(Marc Gravell)的Convert方法可能适用于你。
以下是一个更简单的示例,演示了你的问题。
using System;
using System.Linq.Expressions;

namespace Program
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Expression<Func<CommentUrlCommon, bool>> predicate = f => f.Id == 1;

            //As you know this doesn't work
            //Expression converted = Expression.Convert(predicate, typeof(Expression<Func<CommentUrl, bool>>));

            //this doesn't work either...
            Expression converted2 = Expression.Convert(predicate, typeof(Expression<Func<CommentUrlCommon, bool>>));

            Console.ReadLine();
        }
    }

    public class CommentUrlCommon
    {
        public int Id { get; set; }
    }

    public class CommentUrl
    {
        public int Id { get; set; }
    }
}

你提供的链接中提供的解决方案非常好。谢谢! - Teoman shipahi
直接从Linq查询构建System.Linq.Expressions.Expression的绝佳示例。以前我尝试通过反射来实现,但那并不愉快。 - phil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接