【发布时间】:2016-11-12 17:49:42
【问题描述】:
我正在构建一个辅助方法,该方法基于一个属性返回一个表达式,该属性可用于 orderby 或 where 等。在 linq to entity 操作中。
我不会预先知道属性的类型,所以我将其声明为对象和动态,并且还尝试使用Expression.Convert,但它不适用于非字符串类型的属性。
我正在使用的不是字符串的当前属性类型是int?,我得到的错误是
“System.Nullable`1[System.Int32]”类型的表达式不能用于返回类型“System.Object”;
代码:
var param = Expression.Parameter(typeof(Employee), "x");
MemberExpression propExp = Expression.Property(param, "somePropertyName");
Expression.Lambda<Func<Employee, object>>(propExpression, param);
正如我所说,我在上面使用了 object 和 dynamic 并获得了相同的结果。我也尝试将其转换为正确的类型,但这不起作用:
Expression conversion = Expression.Convert(propExp, ((PropertyInfo)propExp.Member).PropertyType)
当我处于调试模式并尝试此操作时,Expression.Lambda(conversiona, param),它似乎可以工作
{x => Convert(x.EmployeeNo)}
Body: {Convert(x.EmployeeNo)}
CanReduce: false
DebugView: ".Lambda #Lambda1<System.Func`2[xx.DomainModel.Entities.Employee,System.Nullable`1[System.Int32]]>(xx.DomainModel.Entities.Employee $x)\r\n{\r\n (System.Nullable`1[System.Int32])$x.EmployeeNo\r\n}"
Name: null
NodeType: Lambda
Parameters: Count = 1
ReturnType: {Name = "Nullable`1" FullName = "System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}
TailCall: false
Type: {Name = "Func`2" FullName = "System.Func`2[[xx.DomainModel.Entities.Employee, Fng.Facts.DomainModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}
...但是当我在 orderby 中使用它时,我得到了
错误 CS1503:参数 2:无法从 'System.Linq.Expressions.LambdaExpression' 转换为 'string'
我想也许我需要在检查时使用正确的类型动态构建 Expression.Lambda,但还没有尝试过
感谢您的帮助
【问题讨论】:
-
与其尝试构建表达式,不如尝试构建整个
OrderBy(Descending)/ThenBy(Descending)调用。否则,您将遇到与How can I specify a predicate's type, which I won't know until runtime? 相同的问题
标签: c# entity-framework linq expression-trees