Ecy*_*yrb 5 .net c# linq-to-sql
编辑:让我们再试一次.这次我使用了AdventureWorks示例数据库,所以你可以一起玩.这将排除我在自己的数据库中所做的任何疯狂.这是一个新的例子,展示了什么有用,我期望工作(但没有).任何人都可以解释为什么它不起作用或建议一种不同的方式来实现我的目标(重构共同的表达,以便它可以在其他地方重用)?
using (AdventureWorksDataContext db = new AdventureWorksDataContext())
{
// For simplicity's sake we'll just grab the first result.
// The result should have the name of the SubCategory and an array of Products with ListPrice greater than zero.
var result = db.ProductSubcategories.Select(subCategory => new
{
Name = subCategory.Name,
ProductArray = subCategory.Products.Where(product => product.ListPrice > 0).ToArray()
}).First();
Console.WriteLine("There are {0} products in SubCategory {1} with ListPrice > 0.", result.ProductArray.Length, result.Name);
// Output should say: There are 3 products in SubCategory Bib-Shorts with ListPrice > 0.
// This won't work. I want to pull the expression out so that I can reuse it in several other places.
Expression<Func<Product, bool>> expression = product => product.ListPrice > 0;
result = db.ProductSubcategories.Select(subCategory => new
{
Name = subCategory.Name,
ProductArray = subCategory.Products.Where(expression).ToArray() // This won't compile because Products is an EntitySet<Product> and that doesn't have an overload of Where that accepts an Expression.
}).First();
Console.WriteLine("There are {0} products in SubCategory {1} with ListPrice > 0.", result.ProductArray.Length, result.Name);
}
Run Code Online (Sandbox Code Playgroud)
</Edit>
以下LINQ to SQL工作正常:
var result = from subAccount in db.SubAccounts
select new ServiceTicket
{
MaintenancePlans = subAccount.Maintenances.Where(plan => plan.CancelDate == null && plan.UpgradeDate == null).Select(plan => plan.ToString()).ToArray()
// Set other properties...
};
Run Code Online (Sandbox Code Playgroud)
但是,我想打破传递给Where它的谓词,因为它在整个代码中使用了.但是如果我尝试将定义的谓词传递给Where它失败,例如:
Func<DatabaseAccess.Maintenance, bool> activePlanPredicate = plan => plan.CancelDate == null && plan.UpgradeDate == null;
var result = from subAccount in db.SubAccounts
select new ServiceTicket
{
MaintenancePlans = subAccount.Maintenances.Where(activePlanPredicate).Select(plan => plan.ToString()).ToArray()
// Set other properties...
};
Run Code Online (Sandbox Code Playgroud)
这对我来说毫无意义.谁能解释一下发生了什么? Maintenances是类型的EntitySet<DatabaseAccess.Maintenance>.我得到的错误是:
System.NotSupportedException:用于查询运算符'Where'的不支持的重载.
编辑:对于那些感兴趣的人,这里是Reflector对于优化设置为.NET 2.0的第一个(工作)示例的内容:
using (BugsDatabaseDataContext db = new BugsDatabaseDataContext())
{
ParameterExpression CS$0$0001;
ParameterExpression CS$0$0006;
ParameterExpression CS$0$0010;
return db.SubAccounts.Select<SubAccount, ServiceTicket>(Expression.Lambda<Func<SubAccount, ServiceTicket>>(
Expression.MemberInit(
Expression.New(
(ConstructorInfo) methodof(ServiceTicket..ctor),
new Expression[0]),
new MemberBinding[]
{
Expression.Bind(
(MethodInfo) methodof(ServiceTicket.set_MaintenancePlans),
Expression.Call(
null,
(MethodInfo) methodof(Enumerable.ToArray),
new Expression[]
{
Expression.Call(
null,
(MethodInfo) methodof(Enumerable.Select),
new Expression[]
{
Expression.Call(
null,
(MethodInfo) methodof(Enumerable.Where),
new Expression[]
{
Expression.Property(CS$0$0001 = Expression.Parameter(typeof(SubAccount), "subAccount"), (MethodInfo) methodof(SubAccount.get_Maintenances)),
Expression.Lambda<Func<Maintenance, bool>>(
Expression.AndAlso(
Expression.Equal(
Expression.Property(CS$0$0006 = Expression.Parameter(typeof(Maintenance), "plan"), (MethodInfo) methodof(Maintenance.get_CancelDate)),
Expression.Convert(Expression.Constant(null, typeof(DateTime?)), typeof(DateTime?)), false, (MethodInfo) methodof(DateTime.op_Equality)
),
Expression.Equal(
Expression.Property(CS$0$0006, (MethodInfo) methodof(Maintenance.get_UpgradeDate)),
Expression.Convert(Expression.Constant(null, typeof(DateTime?)), typeof(DateTime?)), false, (MethodInfo) methodof(DateTime.op_Equality)
)
),
new ParameterExpression[] { CS$0$0006 }
)
}
),
Expression.Lambda<Func<Maintenance, string>>(
Expression.Call(
CS$0$0010 = Expression.Parameter(typeof(Maintenance), "plan"),
(MethodInfo) methodof(object.ToString),
new Expression[0]
),
new ParameterExpression[] { CS$0$0010 }
)
}
)
}
)
)
}
),
new ParameterExpression[] { CS$0$0001 }
)
).ToList<ServiceTicket>();
}
Run Code Online (Sandbox Code Playgroud)
编辑:第二个示例(使用谓词)的Reflector输出大致相似.最大的区别在于,在通话中Enumerable.Where,而不是通过Expression.Lambda它Expression.Constant(activePlanPredicate).
我不完全理解 Linq to Entities 的本质,但是有一个专门为帮助解决这个问题而设计的开源(可在专有软件中使用)工具包,称为 LinqKit,链接到这篇与 O'Reilly 相关的文章:
http://www.albahari.com/nutshell/predicatebuilder.aspx
由于我不太明白其中的内容,我就简单引用一下:
实体框架的查询处理管道无法处理调用表达式,这就是您需要对查询中的第一个对象调用 AsExpandable 的原因。通过调用 AsExpandable,您可以激活 LINQKit 的表达式访问者类,该类用实体框架可以理解的更简单的构造替换调用表达式。
以下是该项目支持的代码类型:
using LinqKit;
// ...
Expression<Func<Product, bool>> expression = product => product.ListPrice > 0;
var result = db.ProductSubcategories
.AsExpandable() // This is the magic that makes it all work
.Select(
subCategory => new
{
Name = subCategory.Name,
ProductArray = subCategory.Products
// Products isn't IQueryable, so we must call expression.Compile
.Where(expression.Compile())
})
.First();
Console.WriteLine("There are {0} products in SubCategory {1} with ListPrice > 0."
, result.ProductArray.Count()
, result.Name
);
Run Code Online (Sandbox Code Playgroud)
结果是:
子类别背带裤中有 3 件产品,其列表价格 > 0。
是的,也不例外,我们可以提取谓词!
| 归档时间: |
|
| 查看次数: |
1619 次 |
| 最近记录: |