How to speed up my foreach loop

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
suppose this is my foreach loop which is nested

foreach (var data in OrderWiseLineItem)
{
// string Li = data1.LineItem;
string section = data.Section;
string Li = data.Lineitem;

if (!String.IsNullOrEmpty(Li) && !String.IsNullOrEmpty(section))
{
// for broker row
foreach (var broker in DistinctBroker)
{
rowNumber = rowNumber + 1;
brokerRowWeightageRowNumber = new WeightageRowNumber();
brokerRowWeightageRowNumber.Section = section;
brokerRowWeightageRowNumber.Lineitem = Li;
brokerRowWeightageRowNumber.Broker = broker;
brokerRowWeightageRowNumber.RowNumber = rowNumber;
brokerRowWeightageRowNumber.Weightage = (int)RowWeightage.BrokerRow;
WeightageRowNumberall.Add(brokerRowWeightageRowNumber);

}


// for Consensus row .... weightage 2 (red color)
rowNumber = rowNumber + 1;
ConsensusRowWeightageRowNumber = new WeightageRowNumber();
ConsensusRowWeightageRowNumber.Section = section;
ConsensusRowWeightageRowNumber.Lineitem = Li;
ConsensusRowWeightageRowNumber.Broker = "";
ConsensusRowWeightageRowNumber.RowNumber = rowNumber;
ConsensusRowWeightageRowNumber.Weightage = (int)RowWeightage.ConsenSusRow;
WeightageRowNumberall.Add(ConsensusRowWeightageRowNumber);


if (qcTrueDistin.Any(x => x.TabName.Equals(section) && x.StandardLineItem.Equals(Li)))
{
// for QC Check row .... weightage 3, if any (yellow color)
foreach (var broker in DistinctBroker)
{

//Interlocked.Increment(ref rowNumber);
rowNumber = rowNumber + 1;
QcRowWeightageRowNumber = new WeightageRowNumber();

QcRowWeightageRowNumber.Section = section;
QcRowWeightageRowNumber.Lineitem = Li;
QcRowWeightageRowNumber.Broker = broker;
QcRowWeightageRowNumber.RowNumber = rowNumber;
QcRowWeightageRowNumber.Weightage = (int)RowWeightage.QcRow;
WeightageRowNumberall.Add(QcRowWeightageRowNumber);
}
}
}

}

when i am iterating in huge data then it is taking time so i used parallel.foreach which causing problem that data is getting overlap which produce wrong data.

instead of using parallel foreach any way exist to speed up my nested foreach loop ?

linq foreach is faster than regular foreach. i am talking about this kind of linq foreach

someList.ForEach(x => { if(x.RemoveMe) someList.Remove(x); });

tell me linq foreach is faster than normal foreach ?

please guide me. thanks

Continue reading...
 
Back
Top