C# 表达式强制转换错误-类型之间未定义强制运算符
在我的数据存储库中,我有一个基类和派生类,如下所示C# 表达式强制转换错误-类型之间未定义强制运算符,c#,linq,expression,C#,Linq,Expression,在我的数据存储库中,我有一个基类和派生类,如下所示 public abstract class RepositoryBase<T> : IRepository<T> where T : EntityBase { public async Task<T> FindOneAsync(Expression<Func<T, bool>> predicate) { List<T> list = awai
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);
}
}
公共抽象类RepositoryBase:IRepository其中T:EntityBase
{
公共异步任务FindOneAsync(表达式谓词)
{
List List=等待搜索同步(谓词);
返回列表。FirstOrDefault();
}
}
公共类CommentUrlRepository:RepositoryBase,ICommentUrlRepository
{
公共异步任务FindOneAsync(
表达式谓词
)
{
表达式lambda=Cast(谓词);
CommentUrl CommentUrl=等待FindOneSync(lambda);
返回MappingManager.Map(commentUrl);
}
私人表达演员(
表达式谓词
)
{
表达式转换=
表达式。转换(
谓语
类型(表达式)
);
//抛出异常
//类型之间未定义强制运算符
返回表达式.Lambda(已转换,谓词.Parameters);
}
}
当我点击“Cast”功能时,我得到以下错误:
在类型“System.Func`2[CommentUrlCommon,System.Boolean]”和“System.Linq.Expressions.Expression`1[System.Func`2[CommentUrl,System.Boolean]]”之间未定义强制运算符
我怎样才能转换这个表达式值呢?我想你想要的是做不到的…
查看更多信息。
如果幸运的话,您的表达式很简单,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; }
}
}
使用系统;
使用System.Linq.Expressions;
名称空间程序
{
内部课程计划
{
私有静态void Main(字符串[]args)
{
表达式谓词=f=>f.Id==1;
//正如你所知,这是行不通的
//Expression converted=Expression.Convert(谓词,typeof(表达式));
//这也不行。。。
Expression converted2=Expression.Convert(谓词,typeof(表达式));
Console.ReadLine();
}
}
公共类Common
{
公共int Id{get;set;}
}
公共类注释URL
{
公共int Id{get;set;}
}
}
您必须拆分并重新生成表达式,至少要替换其参数(可能还有参数上的任何成员访问表达式)。这可能是一项艰苦的工作,而且很容易出错。您可以(也可能应该)通过不公开任何接受表达式的方法来避免这样做。请使用更专业的方法(即FindOneAsync(int primaryKey)
),您提供的链接中提供的解决方案非常有效。谢谢直接从Linq查询构建System.Linq.Expressions.Expression的绝佳示例。以前我试图通过反思来做到这一点,但这并不令人愉快