我正在尝试构建一个表达式树,以使用EF4在.NET 4.0中执行LINQ to Entities查询。当我尝试执行已建立的查询时NotSupportedException
,出现以下消息:
LINQ to Entities无法识别方法'System.Data.Objects.ObjectQuery`1 [TestWpf.Customer] Where(System.String,System.Data.Objects.ObjectParameter [])'方法,并且该方法无法转换为存储表达式。
我正在查询罗斯文数据库。我的实体是从数据库生成的。在下面的代码中,我具有要在方法中构建的查询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();
}
要构造您正在使用的特定查询,请尝试以下操作:
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);
}
通常,要访问LINQ扩展方法(例如Where),您必须查看Queryable
该类:
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));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句