简体   繁体   English

如何在Foreach循环中更新IList中的记录?

[英]How to update records from an IList in a Foreach loop?

My controller is passing through a list which I then need to loop through and update every record in the list in my database. 我的控制器正在通过一个列表,然后我需要循环并更新数据库中列表中的每个记录。 I'm using ASP.NET MVC with a repository pattern using Linq to Sql. 我正在使用ASP.NET MVC和使用Linq to Sql的存储库模式。 The code below is my save method which needs to add a record to an invoice table and then update the applicable jobs in the job table from the db. 下面的代码是我的save方法,它需要将记录添加到发票表中,然后从db更新作业表中的适用作业。

public void SaveInvoice(Invoice invoice,  IList<InvoiceJob> invoiceJobs)
{
    invoiceTable.InsertOnSubmit(invoice);
    invoiceTable.Context.SubmitChanges();

    foreach (InvoiceJob j in invoiceJobs)
    {
        var jobUpdate = invoiceJobTable.Where(x => x.JobID == j.JobID).Single();
        jobUpdate.InvoiceRef = invoice.InvoiceID.ToString();
        invoiceJobTable.GetOriginalEntityState(jobUpdate);
        invoiceJobTable.Context.Refresh(RefreshMode.KeepCurrentValues, jobUpdate);
        invoiceJobTable.Context.SubmitChanges();
    }
}

**I've stripped the code down to just the problem area. **我已将代码删除到问题区域。

This code doesn't work and no job records are updated, but the invoice table is updated fine. 此代码不起作用,并且没有更新作业记录,但发票表更新正常。 No errors are thrown and the invoiceJobs IList is definitely not null. 不会抛出任何错误,invoiceJobs IList绝对不是null。 If I change the code by removing the foreach loop and manually specifying which JobId to update, it works fine. 如果我通过删除foreach循环并手动指定要更新哪个JobId来更改代码,它可以正常工作。 The below works: 以下作品:

public void SaveInvoice(Invoice invoice,  IList<InvoiceJob> invoiceJobs)
{
    invoiceTable.InsertOnSubmit(invoice);
    invoiceTable.Context.SubmitChanges();

    var jobUpdate = invoiceJobTable.Where(x => x.JobID == 10000).Single();
    jobUpdate.InvoiceRef = invoice.InvoiceID.ToString();
    invoiceJobTable.GetOriginalEntityState(jobUpdate);
    invoiceJobTable.Context.Refresh(RefreshMode.KeepCurrentValues, jobUpdate);
    invoiceJobTable.Context.SubmitChanges();
}

I just can't get the foreach loop to work at all. 我根本无法让foreach循环工作。 Does anyone have any idea what I'm doing wrong here? 有谁知道我在这里做错了什么?

It seems like the mostly likely cause of this problem is that the invokeJobs collection is an empty collection. 看起来这个问题最invokeJobs原因是invokeJobs集合是一个空集合。 That is it has no elements hence the foreach loop effectively does nothing. 也就是说它没有元素,因此foreach循环实际上没有任何作用。

You can verify this by adding the following to the top of the method (just for debugging purposes) 您可以通过在方法顶部添加以下内容来验证这一点(仅用于调试目的)

if (invoiceJobs.Count == 0) {
  throw new ArgumentException("It's an empty list");
}

Change this 改变这个

        var jobUpdate = invoiceJobTable.Where(x => x.JobID == 10000).Single();
        jobUpdate.InvoiceRef = invoice.InvoiceID.ToString();
        invoiceJobTable.GetOriginalEntityState(jobUpdate);
        invoiceJobTable.Context.Refresh(RefreshMode.KeepCurrentValues, jobUpdate);
        invoiceJobTable.Context.SubmitChanges();

to

        var jobUpdate = invoiceJobTable.Where(x => x.JobID == 10000).Single();
        jobUpdate.InvoiceRef = invoice.InvoiceID.ToString();
        invoiceJobTable.SubmitChanges();

It looks like your GetOriginalEntityState doesn't actually do anything, because you don't use the returned value. 看起来你的GetOriginalEntityState实际上没有做任何事情,因为你没有使用返回的值。 I can't see any reason why you are making the DataContext.Refresh() call. 我看不出你为什么要进行DataContext.Refresh()调用。 All it does is erase the changes you made, thus making your "foreach loop not work" 它只是擦除你所做的改变,从而使你的“foreach循环不起作用”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM