2

我正在尝试构建一个表达式树以在 .NET 4.0 中使用 EF4 执行 LINQ to Entities 查询。当我尝试执行已构建的查询时,我收到NotSupportedException以下消息:

LINQ to Entities 无法识别方法 'System.Data.Objects.ObjectQuery`1[TestWpf.Customer] Where(System.String, System.Data.Objects.ObjectParameter[])' 方法,并且此方法无法转换为存储表达式。

我正在查询 Northwind 数据库。我的实体是从数据库中生成的。在下面的代码中,我尝试在方法中构建查询GetQuery1(),并且尝试在方法中构建它GetQuery2()

如果我设置断点并检查query1变量,它的Expression属性是:

Convert(value(System.Data.Objects.ObjectSet`1[TestWpf.Customer])).MergeAs(AppendOnly).Where(c => c.CompanyName.Contains("z"))

这是Convert().MergeAs(AppendOnly)在做什么?我试图在 MSDN 上搜索,但找不到我需要的东西(至少我认为我找不到它......)。另外,我做错了什么?

我认为也许我调用了一个不正确的Where()方法,因为 Intellisense 说还有另一种方法,它是一种扩展方法。我没有尝试更新whereMethod变量以获取该变量,但我也不确定如何。

private static IQueryable<Customer> GetQuery1(NorthEntities context) {
    return context.Customers.Where(c => c.CompanyName.Contains("z"));
}

private static IQueryable<Customer> GetQuery2(NorthEntities context) {
    var custParam = Expression.Parameter(typeof(Customer), "c");
    var custCollection = Expression.Constant(context.Customers);
    var companyNamePropValue = Expression.Property(custParam, typeof(Customer).GetProperty("CompanyName"));
    var containsParameter = Expression.Constant("z");
    var containsMethod = Expression.Call(companyNamePropValue, typeof(string).GetMethod("Contains"), containsParameter);
    var whereMethod = context.Customers.GetType().GetMethod("Where", new Type[] { typeof(string), typeof(ObjectParameter[]) });
    var param2 = Expression.Constant(new ObjectParameter[] { });
    var where = Expression.Call(custCollection, whereMethod, companyNamePropValue, param2);
    return ((IQueryable<Customer>)context.Customers).Provider.CreateQuery<Customer>(where);
}

private static void Main(string[] args) {
    using (var context = new NorthEntities()) {
        var query1 = GetQuery1(context);
        var query2 = GetQuery2(context);

        foreach (var c in query1)
            Console.WriteLine(c.CompanyName);
        foreach (var c in query2)
            Console.WriteLine(c.CompanyName);
    }

    Console.ReadLine();
}
4

1 回答 1

2

To construct the specific query you're working with, try the following:

private static IQueryable<Customer> GetQuery2(NorthEntities context) {
    IQueryable<Customer> customers = context.Customers;
    var custParam = Expression.Parameter(typeof(Customer), "c");
    var companyNamePropValue = Expression.Property(custParam, typeof(Customer).GetProperty("CompanyName"));
    var containsParameter = Expression.Constant("z");
    var containsCall = Expression.Call(companyNamePropValue, typeof(string).GetMethod("Contains"), containsParameter);
    var wherePredicate = Expression.Lambda<Func<Customer, bool>>(containsCall, custParam);
    return customers.Where(wherePredicate);
}

In general, to get access to the LINQ extension methods (e. g. Where), you'll have to look in the Queryable class:

var genericWhereMethod = typeof(Queryable).GetMethods()
    .Single(m => m.Name == "Where" 
        // distinguishes between Where((T, int) => bool) and Where(T => bool)
        && m.GetParameters()[1].ParameterType
           .GetGenericArguments()[0].GetGenericTypeDefinition() == typeof(Func<,>));

// make the Where method that applies to IQueryable<Customer>
var whereMethod = genericWhereMethod.MakeGenericMethod(typeof(Customer));
于 2013-07-06T15:11:14.730 回答