Linq2SQL"本地序列不能在LINQ to SQL中使用"错误

Ant*_*ott 8 linq linq-to-sql

我有一段代码,它将内存列表与数据库中保存的一些数据相结合.这在我的单元测试中运行得很好(使用使用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;
    }
Run Code Online (Sandbox Code Playgroud)

当我针对数据库运行它时,我收到的错误我真的不明白.

异常信息:
    异常类型:System.NotSupportedException
    异常消息:除Contains()运算符外,本地序列不能用于查询运算符的LINQ to SQL实现.

我究竟做错了什么?

我猜这与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;
    }
Run Code Online (Sandbox Code Playgroud)

Abe*_*ler 15

我猜这与orderItems在数据库上并且releasedItems在内存中的事实有关.

你是对的,你不能使用LINQ将表连接到List.

看看这个链接:

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

他建议使用Contains()方法,但你必须使用它来查看它是否适合你的需要.