c# - Parallel.Foreach 给出错误 "Index was outside the bounds of the array "

标签 c# nhibernate parallel.foreach

我在 parallel.foreach 中遇到了一些问题,即“索引超出了数组的范围”。我附上了 parallel.foreach 的一些代码以及崩溃的地方。

 var lstFRItems = session.CreateCriteria<TFRItem>().Add(Restrictions.Eq("TSCEnterprise.FEnterpriseID", EnterpriseId)).AddOrder(Order.Asc("FName")).List<TFRItem>();
                    List<FRItemAccount> lstItemAccount = new List<FRItemAccount>();
                    var ListAccounts = session.CreateCriteria<TFRItemAccount>().List<TFRItemAccount>();   //lstFRItems.Select(i => new  { i.TFRItemAccounts }).ToList();
                    //foreach (var item in lstFRItems)
                Parallel.ForEach(lstFRItems, item =>
                 {
                     var lstItemAcc = ListAccounts.Where(i => i.TFRItem == item); //item.TFRItemAccounts.ToList();
                     FRItemAccount account = new FRItemAccount();
                     account.ItemID = item.FItemID;
                     account.ItemAccount = new List<ItemAccount>();
                   //  foreach (var itemAcct in lstItemAcc)
                    Parallel.ForEach(lstItemAcc, itemAcct =>
                     {
                         ItemAccount oItemAccount = new ItemAccount();
                         if (itemAcct != null)
                         {
                             oItemAccount.ItemAccountID = itemAcct.FItemAccountID;

                             if (itemAcct.TSCProperty == null)
                             {
                                 oItemAccount.ForID = itemAcct.TSCCompany.FCompanyID;
                                 oItemAccount.ForCompanyName = "Co#  " + "- " + itemAcct.TSCCompany.FID + " " + itemAcct.TSCCompany.FName;
                                 oItemAccount.FID = itemAcct.TSCCompany.FID;
                                 oItemAccount.ForType = 1;
                             }
                             else
                             {
                                 oItemAccount.ForID = itemAcct.TSCProperty.FPropertyID;
                                 oItemAccount.ForCompanyName = "Prop#  " + "- " + itemAcct.TSCProperty.FID + " " + itemAcct.TSCProperty.FName;
                                 oItemAccount.FID = itemAcct.TSCProperty.FID;
                                 oItemAccount.ForType = 2;
                             }
                             oItemAccount.Account = itemAcct.FAccount;
                             account.GLAccount = itemAcct.FAccount.ToString("#0.000"); //Formatted by Lhore Bansal
                            // account.Account = itemAcct.FAccount;
                             oItemAccount.isExisting = true;
                             //Original TFRItemAccount
                             oItemAccount.orgItemAccount = itemAcct;
                         }
                         if (lstItemAcc == null)
                             account.ItemID = item.FItemID;
                         account.ItemAccount.Add(oItemAccount);
                     });
                     //Original tFRItem
                     account.Item = item;
                     //account.BaseAccount = Convert.ToDouble(item.FBaseAccount.ToString("F0")); // commented by jeet
                     account.BaseAccount = Convert.ToDouble((int)item.FBaseAccount); // added by jeet
                     account.Name = item.FName;
                     account.Type = item.FType;
                     lstItemAccount.Add(account);
                 });
                  //  tx.Commit();
                    return Item = lstItemAccount;

它在倒数第三行“lstItemAccount.Add(account)”处崩溃。当我在 lstItemAccount 中看到时,它有一些计数,并且在 base 部分它有一个错误 "base {System.SystemException} = {"Source array was not long enough.检查 srcIndex 和长度,以及数组的下限。"}"。

这个错误的解决方法是什么?

最佳答案

我会使用 ConcurrentBag<T>而不是 List<T> . List<T>专为仅一个线程的访问而设计。

关于c# - Parallel.Foreach 给出错误 "Index was outside the bounds of the array ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20245919/

相关文章:

c# - NHibernate session ObjectDisposedException

c# - 如何并行化 IEnumerable 的 IEnumerable?

C# Parallel.ForEach XslCompiledTransform 与 Saxon 9.7.0.6 HE

c# - 使用 lambda 表达式获取数组元素相等的子集

c# - 在 Visual Studio Code 中引用 DLL

c# - 列表中的列表(模型)工厂

c# - 为什么简单的 Parallel.ForEach 有时无法将值添加到字符串变量?

c# - Extjs Ajax 文件下载请求 C# MVC

c# - 选项卡控制选项卡将仅使用等待光标

c# - Nhibernate - 在 QueryOver.IsLike 中使用列值