【发布时间】:2014-02-28 19:21:38
【问题描述】:
我的问题是我需要查询泛型类中属性的值。该属性带有一个属性标记。
见以下代码:
var rowKeyProperty = EFUtil.GetClassPropertyForRowKey<T>();
var tenantKeyProperty = EFUtil.GetClassPropertyForTenantKey<T>();
var queryResult =
objContext.CreateObjectSet<T>().Single(l => (((int) tenantKeyProperty.GetValue(l, null)) == tenantKey) &&
(((int)rowKeyProperty.GetValue(l, null)) == KeyValue));
rowKeyProperty 和tenantKeyProperty 的类型是System.Reflection.PropertyInfo。
我明白为什么我会收到错误消息。 linq查询翻译成SQL时,无法理解property.GetValue。
但是,我对这里的工作完全感到困惑。有谁知道如何实现这一目标?谢谢。
【问题讨论】:
-
使用反射属性构建与 EF 兼容的查询的唯一方法是自己构建表达式树。
-
您如何确定需要获取哪些属性?
-
有一个与属性关联的属性。我调用attribute.isdefined 返回该属性为真。
标签: c# linq entity-framework-6