【发布时间】:2010-12-28 13:37:19
【问题描述】:
我正在尝试创建一个函数,将“where”子句添加到基于属性和值的查询中。这是我的函数的一个非常简单的版本。
Private Function simplified(ByVal query As IQueryable(Of T), ByVal PValue As Long, ByVal p As PropertyInfo) As ObjectQuery(Of T)
query = query.Where(Function(c) DirectCast(p.GetValue(c, Nothing), Long) = PValue)
Dim t = query.ToList 'this line is only for testing, and here is the error raise
Return query
End Function
错误消息是:LINQ to Entities 无法识别方法“System.Object CompareObjectEqual(System.Object, System.Object, Boolean)”方法,并且该方法无法转换为存储表达式。
看起来不能在 linq 查询中使用 GetValue。我可以通过其他方式实现吗?
在 C#/VB 中发布您的答案。选择让您感觉更舒适的那个。
谢谢
编辑:我也试过这段代码,结果相同
Private Function simplified2(ByVal query As IQueryable(Of T))
query = From q In query
Where q.GetType.GetProperty("Id").GetValue(q, Nothing).Equals(1)
Select q
Dim t = query.ToList
Return query
End Function
【问题讨论】:
标签: vb.net linq generics linq-to-entities