是否可以将类型y的表达式转换或强制转换为x?
类型的表达式class y
public class y
{
public int ID { get; set; }
}
class x
public class x
{
public int ID { get; set; }
}
表达
Expression<Func<y, bool>>
如何将其转换/转换为
var t = (Expression<Func<x, bool>>)Expression<Func<y, bool>>
由于 锐
答案 0 :(得分:0)
如果x
和y
之间没有关系,则不能,至少不能作为演员。
您可以做的是提供从x
到y
的转换。通常,这样的事情:
public static Expression<Func<TB, TReturn>> ConvertExpression<TA, TB, TReturn>(
Expression<Func<TA, TReturn>> expr,
Func<TB, TA> converter
) {
return (TB input) => expr.Compile()(converter(input));
}
然后,要实际转换表达式,您需要提供从y
到x
的转换:
ConvertExpression(yourExpressionX, yVal => new x {ID = yVal.ID});