创建正确的 lambda 表达式时遇到问题
这是我需要更改的代码:
var xParam = Expression.Parameter(typeof(E), typeof(E).Name);
MemberExpression leftExpr = MemberExpression.Property(xParam, this._KeyProperty);
Expression rightExpr = Expression.Constant(id);
BinaryExpression binaryExpr = MemberExpression.Equal(leftExpr, rightExpr);
//Create Lambda Expression for the selection
Expression<Func<E, bool>> lambdaExpr = Expression.Lambda<Func<E, bool>>(binaryExpr, new ParameterExpression[] { xParam });
现在我从中得到的表达式是 (x => x.RowId == id)
我想将其更改为 < code>(x => x.RowId) 以便我可以在稍后调用的 ObjectContext.CreateQuery(T)
方法的 OrderBy
中使用它。
有谁知道如何更改上面的代码,以便 lambda 可以正确地在 OrderBy 中使用以按 ID 字段排序?
附注: RowId 来自 this._KeyProperty 我相信。这是使用 Asp.Net MVC 上的实体框架的通用存储库的一部分
This is the code I need to alter:
var xParam = Expression.Parameter(typeof(E), typeof(E).Name);
MemberExpression leftExpr = MemberExpression.Property(xParam, this._KeyProperty);
Expression rightExpr = Expression.Constant(id);
BinaryExpression binaryExpr = MemberExpression.Equal(leftExpr, rightExpr);
//Create Lambda Expression for the selection
Expression<Func<E, bool>> lambdaExpr = Expression.Lambda<Func<E, bool>>(binaryExpr, new ParameterExpression[] { xParam });
Right now the expression I'm getting out of this is (x => x.RowId == id)
and what I want to change it to is (x => x.RowId)
so that I can use it in an OrderBy
for the ObjectContext.CreateQuery(T)
method called later on.
Does anyone know how to change the above code so the lambda is correct to use in an OrderBy to order by the ID field?
Side Notes: The RowId is coming from this._KeyProperty I believe. This is part of a generic repository using the entity framework on Asp.Net MVC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需省略创建常量和“=”:
这假设 _KeyProperty 的类型为“bool”。如果它有不同的类型,只需将 Func更改为到适当的类型。
(编辑以纳入 asgerhallas 和 LukLed 的好建议)
Just omit creating the constant and "=":
This assumes that _KeyProperty has type 'bool'. If it has a different type, just change Func<E, bool> to the appropriate type.
(Edited to incorporate asgerhallas and LukLed's good suggestions)