none
How to use Parallel.Foreach for my scenario RRS feed

  • Question

  • please see my code and tell me how to use Parallel.Foreach for my scenario and my code

                        //Parallel.ForEach(OrderWiseLineItem, data =>
                        foreach (var data in OrderWiseLineItem)
                        {
                            // string Li = data1.LineItem;
                            string section = data.Section;
                            string Li = data.Lineitem;
    
                            if (!String.IsNullOrEmpty(Li) && !String.IsNullOrEmpty(section))
                            {
                                //Parallel.ForEach(DistinctBroker, broker =>
                                foreach (var broker in DistinctBroker)
                                {
                                    //lock (_lock)
                                    //{
                                        // for broker row .... weightage 1 (no color)
                                        rowNumber = rowNumber + 1;
                                        //Interlocked.Increment(ref rowNumber);
                                        brokerRowWeightageRowNumber = new WeightageRowNumber();
                                        brokerRowWeightageRowNumber.Section = section;
                                        brokerRowWeightageRowNumber.Lineitem = Li;
                                        brokerRowWeightageRowNumber.Broker = broker;
                                        brokerRowWeightageRowNumber.RowNumber = rowNumber;
                                        brokerRowWeightageRowNumber.Weightage = (int)RowWeightage.BrokerRow;
                                        //WeightageRowNumberall.Add(brokerRowWeightageRowNumber);
                                        WeightageRowNumberall.Add(brokerRowWeightageRowNumber);
                                    //}
                                }
    
                                //lock (_lock)
                                //{
    
                                    // for Consensus row .... weightage 2 (red color)
                                    rowNumber = rowNumber + 1;
                                    //Interlocked.Increment(ref rowNumber);
                                    ConsensusRowWeightageRowNumber = new WeightageRowNumber();
                                    ConsensusRowWeightageRowNumber.Section = section;
                                    ConsensusRowWeightageRowNumber.Lineitem = Li;
                                    ConsensusRowWeightageRowNumber.Broker = "";
                                    ConsensusRowWeightageRowNumber.RowNumber = rowNumber;
                                    ConsensusRowWeightageRowNumber.Weightage = (int)RowWeightage.ConsenSusRow;
                                    //WeightageRowNumberall.Add(ConsensusRowWeightageRowNumber);
                                    WeightageRowNumberall.Add(ConsensusRowWeightageRowNumber);
                                    //liitemsearchdata.Add(Li + "~" + section + "~" + rowNumber.ToString());
                                    //data.Lineitem + "~" + data.Section + "~" + (data.RowNumber + 1).ToString()
                                //}
    
    
                                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 =>
                                    foreach (var broker in DistinctBroker)
                                    {
                                        //lock (_lock)
                                        //{
    
                                            //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);
                                        //}
    
                                    }
                                }
                            }
    
                        }

    also tell me how to force parallel foreach to init or assign data order wise ?

    i used parallel foreach but getting error in other part of same function. hence my function is too big and that is why i can not paste the whole code rather paste the paralle.foreach part only.

    Thursday, January 17, 2019 3:40 PM

Answers

All replies

  • What is the error you are getting?

    william xifaras

    Thursday, January 17, 2019 4:57 PM
  • See this code which i used.

                        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 for loop i am assigning data to class and at end add that class to list. code is working fine. but code in later section not working fine because data having wrong row number.

    see this below 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);
                                            }
                                        }
                                    }

    this 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;
    

    please guide me how to restructure code for my parallel.ForEach loop as a result i should not get error Sequence contains no elements in later code where i will work with WeightageRowNumberall list.

    looking for help. thanks

    Friday, January 18, 2019 7:53 AM
  • When you get the LINQ error "Sequence contains no elements", this is usually because you are using the First() or Single() command rather than FirstOrDefault() and SingleOrDefault().

    Regarding how to force parallel <g class="gr_ gr_8 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" data-gr-id="8" id="8">foreach</g> to init or assign data - read the MS docs on parallel <g class="gr_ gr_7 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="7" id="7">foreach</g> - specifically how the localInit delegate works.

    Parallel.ForEach Method


    william xifaras

    Friday, January 18, 2019 8:42 PM
  • later i use FirstOrDefault() instead of first() but still getting error some time not every time when executing the code. so it is not clear to me that Parallel.ForEach causes that error ?

    please guide me how to use Parallel.ForEach for my code? should i use one Parallel.ForEach  loop for outer main loop?

    please guide me.

    Saturday, January 19, 2019 8:00 PM
  • It's not clear whether the Parallel.ForEach is causing the error(s), and it will be hard for me or anyone else to diagnose your issue without being able to debug your code.

    You can have nested Parallel.ForEach loops, but is it necessary? A Parallel.ForEach does not necessarily execute in parallel; it is just a request to do so if possible. Therefore, if the execution environment does not have the CPU power to execute the loops in parallel, it will not do so. Is there a reason why you need to to use Parallel.ForEach?

    For now, I would use a regular foreach loop and debug your code. From there, use one Parallel.ForEach for the outer loop if you think you need it.



    william xifaras

    Saturday, January 19, 2019 10:46 PM
  • first see my code. first i compose the code with normal foreach and then no error i encounter.

    the moment i use parallel.foreach then i started getting error some time.

    there was huge iteration and normal foreach was taking long time but when i use parallel.foreach execution was minimize a lot. this is the reason for which i use parallel.foreach.

    one guy see my code and advise

    Not your main problem, but your rowNumbers are going to be all over the place and probably inaccurate the majority of the time. Interlocked.Increment returns the incremented value. Store that in a local variable and assign that to the RowNumber property. Also using lock like that probably doesn't help you. You can pass a state variable into the parallel loops. Use that to build seperare lists and then combine them all at the very end.

    the above statement is not clear to me. if you understand what the person trying to say then please construct my code accordingly and post here.

    thanks

    Sunday, January 20, 2019 11:07 AM
  • Actually, it makes sense. The row numbers will be all over the place. Try doing what he said. Store the Interlocked. Increment in a local variable and assign to the RowNumber.


    Monday, January 21, 2019 6:30 PM
  • that i did but still below line throwing exception object reference not set

    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;

    it is not clear what is going wrong?

    if i use normal foreach instead of parallel.foreach then no problem occur but problem start when i use parallel.foreach. looking for your suggestion that how to construct parallel.foreach for my above code. thanks

    Monday, January 21, 2019 7:18 PM
  • Hi Studip_inn,

    Please try to use one Parallel and one normal loop.

    https://stackoverflow.com/questions/3281604/nested-parallel-foreach-loops-on-the-same-list

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Friday, February 1, 2019 7:39 AM
    • Marked as answer by Sudip_inn Saturday, February 9, 2019 10:30 AM
    Monday, January 28, 2019 7:36 AM
    Moderator