【发布时间】:2015-02-16 06:45:42
【问题描述】:
我正在尝试组合两个工作表达式。
-
left:返回属性值的表达式
Expression<Func<TimeSlot, Guid>> left = x => x.TimeSlotId; -
右:返回常量值的表达式(变量
guid的值)Expression<Func<TimeSlot, Guid>> right = Expression.Lambda<Func<TimeSlot, Guid>> (Expression.Constant(guid, typeof(Guid)), input);
现在我想将这两个表达式打包成另一个表达式,使这两个结果相等。这有效:
// given a TimeSlot slot which guid is equal to the constent
bool eq1 = left.Compile()(slot) == right.Compile()(slot); // true
但这不起作用 ParameterExpression input = Expression.Parameter(typeof(TimeSlot));
Expression<Func<TimeSlot, bool>> expression = Expression.Lambda<Func<TimeSlot, bool>>
(Expression.Equal(left, right), input);
bool eq2 = expression.Compile()(slot); // false
我完全困惑为什么这个结果是不同的。特别是因为这个表达式的 DebugView 看起来像这样:
{Param_0 => (x => x.TimeSlotId == Param_0 => 351155b2-20a5-4c48-8722-ddf0e1f9055a)}
=
.Lambda #Lambda1<System.Func`2[SilverFit.Database.Model.TimeSlot,System.Boolean]>(SilverFit.Database.Model.TimeSlot $var1)
{
.Lambda #Lambda2<System.Func`2[SilverFit.Database.Model.TimeSlot,System.Guid]> == .Lambda #Lambda3<System.Func`2[SilverFit.Database.Model.TimeSlot,System.Guid]>
}
.Lambda #Lambda2<System.Func`2[SilverFit.Database.Model.TimeSlot,System.Guid]>(SilverFit.Database.Model.TimeSlot $x) {
$x.TimeSlotId
}
.Lambda #Lambda3<System.Func`2[SilverFit.Database.Model.TimeSlot,System.Guid]>(SilverFit.Database.Model.TimeSlot $var1) {
.Constant<System.Guid>(351155b2-20a5-4c48-8722-ddf0e1f9055a)
}
有谁知道创建比较表达式结果和常量的表达式的正确方法?
【问题讨论】:
标签: c# lambda expression