Linq .使用 LinqPad 选择方法问题
我无法理解为什么在以下代码中出现错误。我确信我错过了一些简单的东西,但我需要帮助理解。
我一直在使用 LinqToSql 在 LinqPad 中运行此代码。
我在 LinqPad 中有以下代码:
涉及三个表:Shipments、ShipmentDetails 和 ShipmentWeights。所有三个表均通过shipmentID 链接,该ID 是Shipments 表的PK。
在此代码中,最终查询(“权重”)失败并显示错误:“不支持的异常:不支持使用本地集合的查询。”我发现 LinqToSql 中的某些内容不支持使用 .Contains 但我不明白名为“Details”的查询是如何工作的。对我来说这似乎是同一类查询。有人可以帮我填补空白吗?
对于那些不熟悉 LinqPad 的人来说,.Dump 方法只是 IQueryable 上的一个扩展方法,它以格式化的方式打印出内容。
var shipments = Shipments.Where(s => s.OrderID == "Some OrderID");
shipments.Dump("Shipments");
var shipmentIds = shipments.Select(s => s.ShipmentID);
shipmentIds.Dump();
//This query works. What is different about this query than the one that fails?
var shipmentDetails = ShipmentDetails.Where(d => shipmentIds.Contains(d.ShipmentID));
shipmentDetails.Dump("Details");
var detailShipmentIds = shipmentDetails.Select(sd => sd.ShipmentID);
detailShipmentIds.Dump();
//This is the query that generates the error
var shipmentWeights = ShipmentWeights.Where(w => detailShipmentIds.Contains(w.ShipmentID));
shipmentWeights.Dump("Weights");
I'm having trouble understanding why I get an error in the following code. I'm sure I am missing something simple, but I need help understanding.
I have been running this code in LinqPad using LinqToSql.
I have the following code in LinqPad:
There are three tables involved: Shipments, ShipmentDetails, and ShipmentWeights. All three tables are linked by the shipmentID which is the PK of the Shipments table.
In this code, the final query ("Weights") fails with an error: "Not Supported Exception: Queries with local collections are not supported." I gather that something in LinqToSql doesn't support the use of .Contains but I don't understand how the query called "Details" is working. It seems to be the same kind of query to me. Can someone fill in the gaps for me?
For those who are not familiar with LinqPad, the .Dump method is just an extension method on IQueryable that prints out the contents in a formatted fashion.
var shipments = Shipments.Where(s => s.OrderID == "Some OrderID");
shipments.Dump("Shipments");
var shipmentIds = shipments.Select(s => s.ShipmentID);
shipmentIds.Dump();
//This query works. What is different about this query than the one that fails?
var shipmentDetails = ShipmentDetails.Where(d => shipmentIds.Contains(d.ShipmentID));
shipmentDetails.Dump("Details");
var detailShipmentIds = shipmentDetails.Select(sd => sd.ShipmentID);
detailShipmentIds.Dump();
//This is the query that generates the error
var shipmentWeights = ShipmentWeights.Where(w => detailShipmentIds.Contains(w.ShipmentID));
shipmentWeights.Dump("Weights");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的列表是 iQueryable 的 - 即查询尚未执行,因此它们不能用作
Contains
查询的一部分。只需先将它们更改为本地列表即可。例如,对所有本地列表执行相同的操作。
这是完整的清单
Your lists are iQueryable's - ie the queries have not been executed yet so they can't be used as part of the
Contains
query. Simply change them into a local list first and then this will work. egDo the same for all the local lists.
Here's a complete listing
是的,正如所指出的,你必须这样做
Yeah as pointed out, you are going to have to do