C# 平行。Foreach给定错误“;索引超出了数组的边界;
我在parallel.foreach中遇到了一些问题,即“索引超出了数组的边界”。我正在附加一些parallel.foreach的代码,以及它崩溃的地方C# 平行。Foreach给定错误“;索引超出了数组的边界;,c#,nhibernate,parallel.foreach,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>(
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;
var lstFRItems=session.CreateCriteria().Add(Restrictions.Eq(“TSCEnterprise.FEnterpriseID”,EnterpriseId)).AddOrder(Order.Asc(“FName”)).List();
List lstItemAccount=新列表();
var listcounts=session.CreateCriteria().List()//Select(i=>new{i.TFRItemAccounts}).ToList();
//foreach(lstFRItems中的var项目)
Parallel.ForEach(lstFRItems,item=>
{
var lstItemAcc=listcounts.Where(i=>i.TFRItem==item);//item.TFRItemAccounts.ToList();
FriteAccount account=新FriteAccount();
account.ItemID=item.FItemID;
account.ItemAccount=新列表();
//foreach(lstItemAcc中的var itemAcct)
Parallel.ForEach(lstItemAcc,itemAcct=>
{
ItemAccount oItemAccount=新的ItemAccount();
如果(itemAcct!=null)
{
oItemAccount.ItemAccountID=itemAcct.FItemAccountID;
if(itemact.TSCProperty==null)
{
oItemAccount.ForID=itemact.TSCCompany.FCompanyID;
oItemAccount.ForCompanyName=“Co#”+“-”+itemact.TSCCompany.FID+”+itemact.TSCCompany.FName;
oItemAccount.FID=itemact.TSCCompany.FID;
oItemAccount.ForType=1;
}
其他的
{
oItemAccount.ForID=itemAcct.TSCProperty.FPropertyID;
oItemAccount.ForCompanyName=“Prop#”+“-”+itemact.TSCProperty.FID+”+itemact.TSCProperty.FName;
oItemAccount.FID=itemact.TSCProperty.FID;
oItemAccount.ForType=2;
}
oItemAccount.Account=itemAcct.FAccount;
account.GLAccount=itemact.FAccount.ToString(#0.000”)//由Lhore Bansal格式化
//account.account=itemAcct.FAccount;
oItemAccount.isExisting=true;
//原始TFRItemAccount
oItemAccount.orgItemAccount=itemAcct;
}
如果(lstItemAcc==null)
account.ItemID=item.FItemID;
account.itemcount.Add(oItemAccount);
});
//原始格式
账户项目=项目;
//account.BaseAccount=Convert.ToDouble(item.FBaseAccount.ToString(“F0”);//被jeet注释
account.BaseAccount=Convert.ToDouble((int)item.FBaseAccount);//由jeet添加
account.Name=item.FName;
account.Type=item.FType;
lstItemAccount.Add(account);
});
//tx.Commit();
退货项目=LSTIMENTACCOUNT;
它在最后第三行“lstItemAccount.Add(account)”处崩溃。当我在lstItemAccount中看到时,它有一些计数,而在base部分它有一个错误“base{System.SystemException}={”源数组不够长。请检查srcidex和长度,以及数组的下限。“}”。
这个错误的解决方法是什么?它与
ListAccounts.Where(i => i.TFRItem == item);
account.ItemAccount.Add(oItemAccount);
lstItemAccount.Add(account);
列表和数组不是线程安全的。改用我会用
ConcurrentBag
代替列表<代码>列表
仅为一个线程的访问而设计。仅供参考-您可能在lstItemAccount
中遇到线程问题。您是否获得了此问题的有效答案?