IQueryable 中的错误 + foreach

发布于 2024-09-28 18:26:45 字数 871 浏览 3 评论 0原文

我只是想知道是否存在任何已知问题,或者我是否做错了什么。每次我执行如下例所示的操作时,都会收到错误“服务器无法恢复事务”。

我已经问过有关此错误的另一个问题,但现在我发现它只出现在 foreach 循环中

            //Listing orders
            IQueryable<Order> ordersList = ListOrders();

            foreach (Order order in ordersList)
            {
                    if (order.Client_Id != null) //get the exception here.
                    {
                        //some code
                    }
            }

Update:ListOrders() 的代码

    public IQueryable<Order> ListOrders()
    {
        try
        {
            return from o in db.Orders
                   where o.Id == this.Id
                   select o;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message, ex);
        }
    }

I just want to know if there is any known issue, or if I'm doing something wrong. Everytime I do something like the example below, I get an error "the server failed to resume the transaction".

I had already asked another question about this error, but now I've figured out it only appears in the foreach loops

            //Listing orders
            IQueryable<Order> ordersList = ListOrders();

            foreach (Order order in ordersList)
            {
                    if (order.Client_Id != null) //get the exception here.
                    {
                        //some code
                    }
            }

Update: the code for ListOrders()

    public IQueryable<Order> ListOrders()
    {
        try
        {
            return from o in db.Orders
                   where o.Id == this.Id
                   select o;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message, ex);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

許願樹丅啲祈禱 2024-10-05 18:26:45

您的 Client_Id 是延迟加载属性吗?

那么 if (order.Client_Id != null) 检查实际上是在进行某种数据库操作吗?

Is your Client_Id a lazy load property?

So is the if (order.Client_Id != null) check actually doing some kind of database operation?

末が日狂欢 2024-10-05 18:26:45

如果不知道如何处理数据库上下文,就很难确定,因为如果您没有正确使用上下文,则可能会遇到各种问题。为了验证这一点,您可以从列表顺序方法返回一个 IList,如果返回没有错误并且您的 foreach 按预期工作,那么它很可能与您管理上下文的方式有关。

public IList<Order> ListOrders()
{
    try
    {
        return (from o in db.Orders
                where o.Id == this.Id
                select o).ToList();
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message, ex);
    }
}

Without knowing how you are handing your DB Context it is hard to know for sure as if you are not using the context correctly you can have all kind of issues. To verify this, you can return an IList from the list order methods and if this returns with out error and your foreach works as expected then it is most likely related to how you are managing the Context.

public IList<Order> ListOrders()
{
    try
    {
        return (from o in db.Orders
                where o.Id == this.Id
                select o).ToList();
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message, ex);
    }
}
雄赳赳气昂昂 2024-10-05 18:26:45

你可以这样做:

IQueryable<Order> ordersList = ListOrders().where(t=>t.Client_Id != string.Empty);

You can do it like this:

IQueryable<Order> ordersList = ListOrders().where(t=>t.Client_Id != string.Empty);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文