简体   繁体   English

LINQ to Entities 无法识别方法“System.Object GetValue(…)”

[英]LINQ to Entities does not recognize the method 'System.Object GetValue(…)'

My issue is I need to query on the value of a property in a generic class.我的问题是我需要查询泛型类中的属性值。 The property is tagged with an attribute.该属性用属性标记。

See the following code:请参阅以下代码:

 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));

The rowKeyProperty and tenantKeyProperty are of type System.Reflection.PropertyInfo. rowKeyProperty 和tenantKeyProperty 属于System.Reflection.PropertyInfo 类型。

I understand why I am getting the error.我明白为什么我收到错误。 When the linq query is translated to SQL, it can't understand the property.GetValue.当linq查询被翻译成SQL时,它无法理解property.GetValue。

However, I'm completely stumped as to a work around here.但是,我对这里的工作完全感到困惑。 Does anyone have any ideas how to achieve this?有没有人有任何想法如何实现这一目标? Thx.谢谢。

You need to actually build up the Expression objects to represent the expression that you want this to mimic, in this case the expression you want to represent is:您需要实际构建Expression对象来表示您希望它模仿的表达式,在这种情况下,您要表示的表达式是:

l => l.SomeProperty == SomeValue

So you need to build up each component of that bit by bit, from creating the parameter, defining the equality operator, the property access, the constant value, etc.因此,您需要一点一点地构建该组件的每个组件,从创建参数、定义相等运算符、属性访问、常量值等。

public static Expression<Func<TItem, bool>> PropertyEquals<TItem, TValue>(
    PropertyInfo property, TValue value)
{
    var param = Expression.Parameter(typeof(TItem));
    var body = Expression.Equal(Expression.Property(param, property),
        Expression.Constant(value));
    return Expression.Lambda<Func<TItem, bool>>(body, param);
}

Once you have all of that you can call it using the data that you have:一旦你拥有了所有这些,你就可以使用你拥有的数据调用它:

var queryResult = objContext.CreateObjectSet<T>()
    .Where(PropertyEquals<T, int>(tenantKeyProperty, tenantKey))
    .Where(PropertyEquals<T, int>(rowKeyProperty, KeyValue))
    .Single();

Appendix here... Following @Servy answer and based on this topic with a nice answer by @TomBrothers, you can use the same logic to make a StartsWith (or similar) function:附录在这里...遵循@Servy 的回答并基于主题和@TomBrothers 的一个很好的回答,您可以使用相同的逻辑来创建一个StartsWith (或类似的)函数:

public static Expression<Func<TItem, bool>> PropertyStartsWith<TItem>(PropertyInfo propertyInfo, string value)
{
    var param = Expression.Parameter(typeof(TItem));

    var m = Expression.MakeMemberAccess(param, propertyInfo);
    var c = Expression.Constant(value, typeof(string));
    var mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
    var body = Expression.Call(m, mi, c);

    return Expression.Lambda<Func<TItem, bool>>(body, param);
}

In this case, it forces value to be a string.在这种情况下,它强制value为字符串。

It is more correct to specify the type in Expression.Constant(value, typeof(TValue)))Expression.Constant(value, typeof(TValue))) 中指定类型更正确

public static Expression<Func<TItem, bool>> PropertyEquals<TItem, TValue>(
        string property, TValue value)
    {
        var xParameter = Expression.Parameter(typeof(TItem));
        var body = Expression.Equal(Expression.Property(xParameter, property), Expression.Constant(value, typeof(TValue)));
        return Expression.Lambda<Func<TItem, bool>>(body, xParameter);
    }

Or, like this, to check the property.或者,像这样,检查属性。 ChangeType变更类型

public static Expression<Func<TItem, bool>> PropertyEquals<TItem, TValue>(
        string property, TValue value)
    {
        var xParameter = Expression.Parameter(typeof(TItem));
        var type = typeof(TItem).GetProperty(property).PropertyType;
        value = ChangeType<TValue>(value);
        BinaryExpression body = Expression.Equal(Expression.Property(xParameter, property), Expression.Constant(value, type));

        return Expression.Lambda<Func<TItem, bool>>(body, xParameter);
    }

What is it for.它有什么用。 I check all class references to classes, I look for "..ID" entries.我检查所有对类的类引用,我寻找“..ID”条目。 Somewhere I have a type "int" and "int?".某处我有一个类型“int”和“int?”。

public class BudgetLimit : BaseRecord
{
    [Required]
    public int DepartmentID { get; set; } 
    public virtual Department Department { get; set;}

    public int? ProjectID { get; set; }
    public virtual Project Project { get; set; }
 }

You add .AsEnableable after the LINQ statement.在 LINQ 语句之后添加 .AsEnableable。 eg objectdata.AsEnumerable() enter link description here例如 objectdata.AsEnumerable() 在此处输入链接描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ to Entities无法识别方法&#39;System.Object Parse(System.Type,System.String)&#39; - LINQ to Entities does not recognize the method 'System.Object Parse(System.Type, System.String)' LINQ to Entities 无法识别“System.Object get_Item(System.String)”方法 - LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)' method LINQ to Entities无法识别方法&#39;System.String Concat(System.Object)&#39;方法, - LINQ to Entities does not recognize the method 'System.String Concat(System.Object)' method, LINQ to Entities无法识别方法&#39;System.Object get_Item(System.String)&#39; - LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)' LINQ to Entities无法识别方法&#39;Int32 ToInt32(System.Object)&#39;方法 - LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method LINQ to Entities 无法识别方法“System.Object get_Item(System.String)”方法无法转换为存储表达式 - LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)' method cannot be translated into a store expression LINQ to Entities无法识别方法&#39;System.String ToString(System.Object)&#39;方法错误,且字段可为空 - LINQ to Entities does not recognize the method 'System.String ToString(System.Object)' method error occurs with nullable fields 错误:LINQ to Entities无法识别字符串转换时发生的方法&#39;System.String ToString(System.Object)&#39;方法 - Error: LINQ to Entities does not recognize the method 'System.String ToString(System.Object)' method occurs while string conversion LINQ to Entities无法识别方法&#39;Int32 ToInt32(System.Object)&#39;方法,并且此方法无法转换为存储表达式 - LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression 将字符串转换为int时,特定的linq异常。 LINQ to Entities无法识别方法&#39;Int32 ToInt32(System.Object)&#39;m - Specific linq exception when converting string to int. LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' m
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM