Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
767 views
in Technique[技术] by (71.8m points)

vb.net - Help with Linq and Generics. Using GetValue inside a Query

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...