gpt4 book ai didi

Linq2SQL "Local sequence cannot be used in LINQ to SQL"错误

转载 作者:行者123 更新时间:2023-12-04 05:15:14 24 4
gpt4 key购买 nike

我有一段代码将内存中的列表与数据库中保存的一些数据结合在一起。在我的单元测试中,这工作得很好(使用模拟的使用List的Linq2SqlRepository)。

    public IRepository<OrderItem> orderItems { get; set; }

private List<OrderHeld> _releasedOrders = null;
private List<OrderHeld> releasedOrders
{
get
{
if (_releasedOrders == null)
{
_releasedOrders = new List<nOrderHeld>();
}
return _releasedOrders;
}
}

.....

public int GetReleasedCount(OrderItem orderItem)
{
int? total =
(
from item in orderItems.All
join releasedOrder in releasedOrders
on item.OrderID equals releasedOrder.OrderID
where item.ProductID == orderItem.ProductID
select new
{
item.Quantity,
}

).Sum(x => (int?)x.Quantity);

return total.HasValue ? total.Value : 0;
}

我在数据库上运行该错误时,我不太了解。

Exception information:
    Exception type: System.NotSupportedException
    Exception message: Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator.



我究竟做错了什么?

我猜这是因为 orderItems 在数据库中,而 releasedItems 在内存中。

编辑

我已经根据给出的答案更改了代码(谢谢所有)
    public int GetReleasedCount(OrderItem orderItem)
{
var releasedOrderIDs = releasedOrders.Select(x => x.OrderID);

int? total =
(
from item in orderItems.All
where releasedOrderIDs.Contains(item.OrderID)
&& item.ProductID == orderItem.ProductID
select new
{
item.Quantity,
}

).Sum(x => (int?)x.Quantity);

return total.HasValue ? total.Value : 0;
}

最佳答案

I'm guessing it's to do with the fact that orderItems is on the database and releasedItems is in memory.



没错,您不能使用LINQ将表连接到列表。

看一下这个链接:

http://flatlinerdoa.spaces.live.com/Blog/cns!17124D03A9A052B0!455.entry

他建议使用Contains()方法,但是您必须尝试使用​​它,看看它是否可以满足您的需求。

关于Linq2SQL "Local sequence cannot be used in LINQ to SQL"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3132981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com