Linq2SQL“本地序列不能在LINQ to SQL中使用"错误 [英] Linq2SQL "Local sequence cannot be used in LINQ to SQL" error

查看:71
本文介绍了Linq2SQL“本地序列不能在LINQ to SQL中使用"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a piece of code which combines an in-memory list with some data held in a database. This works just fine in my unit tests (using a mocked Linq2SqlRepository which uses List).

    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;
    }

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

I am getting an error I don't really understand when I run it against a database.

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

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 在内存中.

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

我已经根据给出的答案更改了代码(谢谢所有)

I have changed my code based on the answers given (thanks all)

    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;
    }

推荐答案

我猜这与事实有关 该orderItems在数据库上 和releasedItems在内存中.

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

您是正确的,您无法使用LINQ将表连接到列表.

You are correct, you can't join a table to a List using LINQ.

看看这个链接:

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

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

He suggests using the Contains() method but you'll have to play around with it to see if it will work for your needs.

这篇关于Linq2SQL“本地序列不能在LINQ to SQL中使用"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆