Sequence contains no elements issue throwing after implementing Parallel.ForEach loop

245 views Asked by At

I have used nested Parallel.ForEach loop to populate list of data. the after using Parallel.ForEach some time I am getting error Sequence contains no elements which is throwing from later portion of the code.

I have two section in this post. First I will post my Parallel.ForEach loop code

Parallel.ForEach(OrderWiseLineItem, data =>
                    {
                        // string Li = data1.LineItem;
                        string section = data.Section;
                        string Li = data.Lineitem;

                        if (!String.IsNullOrEmpty(Li) && !String.IsNullOrEmpty(section))
                        {
                            Parallel.ForEach(DistinctBroker, broker =>
                            {
                                    // for broker row .... weightage 1 (no color)
                                    
                                    Interlocked.Increment(ref rowNumber);
                                    brokerRowWeightageRowNumber = new WeightageRowNumber();
                                    brokerRowWeightageRowNumber.Section = section;
                                    brokerRowWeightageRowNumber.Lineitem = Li;
                                    brokerRowWeightageRowNumber.Broker = broker;
                                    brokerRowWeightageRowNumber.RowNumber = rowNumber;
                                    brokerRowWeightageRowNumber.Weightage = (int)RowWeightage.BrokerRow;

                                    lock (_lock)
                    {
                                        WeightageRowNumberall.Add(brokerRowWeightageRowNumber);
                                    }
                            }

                                Interlocked.Increment(ref rowNumber);
                                ConsensusRowWeightageRowNumber = new WeightageRowNumber();
                                ConsensusRowWeightageRowNumber.Section = section;
                                ConsensusRowWeightageRowNumber.Lineitem = Li;
                                ConsensusRowWeightageRowNumber.Broker = "";
                                ConsensusRowWeightageRowNumber.RowNumber = rowNumber;
                                ConsensusRowWeightageRowNumber.Weightage = (int)RowWeightage.ConsenSusRow;

                                lock (_lock)
                                {
                                    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)
                                Parallel.ForEach(DistinctBroker, broker =>
                                {

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

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

                                }
                            }
                        }

                    }

In the above code I used Parallel.ForEach because there was huge iteration which was taking few minute and after using Parallel.ForEach loop started taking few second. it was good for me but now some time I am getting Sequence contains no elements error.

Now here I am posting the code which is throwing error Sequence contains no elements.

if (dataBroker.Any(x => !String.IsNullOrEmpty(x.FormulaLiConfig)))
{
    string formulastd = dataBroker.Where(x => !String.IsNullOrEmpty(x.FormulaLiConfig)).FirstOrDefault().FormulaLiConfig;
    List<string> stdformulalist = FormulaevaluationStandardFormula(formulastd);
    foreach (string formulatemp in stdformulalist)
    {
    if (formulatemp.Contains("~"))
    {
        string section1 = formulatemp.Split('~')[0].Trim();
        string li1 = formulatemp.Split('~')[1].Trim();

        int rowNumber1 = -1;
        rowNumber1 = WeightageRowNumberall.Where(x => x.Section.Trim().Equals(section1)
        && x.Lineitem.Trim().Equals(li1)
        && x.Broker.Equals(broker)
        && x.Weightage == (int)RowWeightage.BrokerRow).FirstOrDefault().RowNumber;

        StandardFormulaConstituents objStandardFormulaConstituents = new StandardFormulaConstituents();
        objStandardFormulaConstituents.Id = coutstdformula;
        objStandardFormulaConstituents.Constituent = formulatemp;
        objStandardFormulaConstituents.RowNumberConstituent = rowNumber1;
        coutstdformula = coutstdformula + 1;
        StandardFormulaConstituentsall.Add(objStandardFormulaConstituents);
    }
    }
}             

Actually the exact below line throwing error Sequence contains no elements

rowNumber1 = WeightageRowNumberall.Where(x => x.Section.Trim().Equals(section1) && x.Lineitem.Trim().Equals(li1) 
&& x.Broker.Equals(broker) && x.Weightage == (int)RowWeightage.BrokerRow).FirstOrDefault().RowNumber;

I am not being able to understand the reason why some time I am getting this error Sequence contains no elements from the above line of code. WeightageRowNumberall is populated in Parallel.ForEach that is why I suspect some issue is there in Parallel.ForEach loop.

Please guys see my code and tell me why I am getting this error? Is Parallel.ForEach loop is creating issue as per my code?

Also tell me should I remove nested Parallel.ForEach loop and retain only outer Parallel.ForEach only...does it make sense?

Please guys read my code and give me some solution to restructure my code to get rid of this error Sequence contains no elements.

If I remove Parallel.ForEach and use normal foreach then never encounter the error Sequence contains no elements.

Tell me the reason if anyone understand why I am getting some time error and some time not? I am not being able to capture the reason for the error which is happening not always but some time.

0

There are 0 answers