帮助Linq和泛型。在查询中使用GetValue [英] Help with Linq and Generics. Using GetValue inside a Query

查看:143
本文介绍了帮助Linq和泛型。在查询中使用GetValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图创建一个函数,为基于属性和值的查询添加'where'子句。这是一个非常简单的函数版本。



pre $ 私有函数简化(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'这一行仅用于测试,这里是错误raise
返回查询

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查询
其中q.GetType.GetProperty(Id).GetValue(q,Nothing).Equals(1)
选择q
Dim t = query.ToList
返回查询
End Function


解决方案

您需要将代码转换为表达式树。

  using System; 
使用System.Linq;
使用System.Linq.Expressions;
使用System.Reflection;

命名空间WindowsFormsApplication1
{
静态类程序
{
[STAThread]
static void Main()
{
using(var context = new NorthwindEntities())
{
IQueryable< Customer> query = context.Customers;
query = Simplified&Customer>(查询,CustomerID,ALFKI);
var list = query.ToList();
}
}

静态IQueryable< T>简化< T>(IQueryable< T>查询,字符串propertyName,字符串propertyValue)
{
PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName);
return简化< T>(query,propertyInfo,propertyValue);
}

静态IQueryable< T>简化< T>(IQueryable< T> query,PropertyInfo propertyInfo,string propertyValue)
{
ParameterExpression e = Expression.Parameter(typeof(T),e);
MemberExpression m = Expression.MakeMemberAccess(e,propertyInfo);
ConstantExpression c = Expression.Constant(propertyValue,propertyValue.GetType());
BinaryExpression b = Expression.Equal(m,c);

表达式< Func< T,bool>> lambda = Expression.Lambda< Func< T,bool>>(b,e);
返回query.Where(lambda);
}
}
}


I'm triying to make a function that add a 'where' clause to a query based in a property and a value. This is a very simplefied version of my function.

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

The error message is: LINQ to Entities does not recognize the method 'System.Object CompareObjectEqual(System.Object, System.Object, Boolean)' method, and this method cannot be translated into a store expression.

Looks like a can't use GetValue inside a linq query. Can I achieve this in other way?

Post your answer in C#/VB. Chose the one that make you feel more confortable.

Thanks

EDIT: I also tried this code with the same results

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

解决方案

You need to convert the code to an expression tree.

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace WindowsFormsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            using (var context = new NorthwindEntities())
            {
                IQueryable<Customer> query = context.Customers;
                query = Simplified<Customer>(query, "CustomerID", "ALFKI");
                var list = query.ToList();
            }
        }

        static IQueryable<T> Simplified<T>(IQueryable<T> query, string propertyName, string propertyValue)
        {
            PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName);
            return Simplified<T>(query, propertyInfo, propertyValue);
        }

        static IQueryable<T> Simplified<T>(IQueryable<T> query, PropertyInfo propertyInfo, string propertyValue)
        {
            ParameterExpression e = Expression.Parameter(typeof(T), "e");
            MemberExpression m = Expression.MakeMemberAccess(e, propertyInfo);
            ConstantExpression c = Expression.Constant(propertyValue, propertyValue.GetType());
            BinaryExpression b = Expression.Equal(m, c);

            Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(b, e);
            return query.Where(lambda);
        }
    }
}

这篇关于帮助Linq和泛型。在查询中使用GetValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆