none
Can we use LINQ to populate my object instead of foreach loop RRS feed

  • Question

  • I am using nested foreach loop to populate my object WeightageRowNumberall. foreach taking long time when there is huge data and many iteration.

    this is my foreach code

    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);
                                    }
                                }
                            }
    
                        }


    this way i just populate WeightageRowNumberall for demo purpose but i need to populate at runtime which i did in foreach in above code.

                List<WeightageRowNumber> WeightageRowNumberall = new List<WeightageRowNumber>
                {
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Net Revenue",Broker="BW",Weightage=1,RowNumber=1},
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Net Revenue",Broker="3P-1",Weightage=1,RowNumber=2},
    
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Net Revenue",Broker="",Weightage=2,RowNumber=3},
    
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Net Revenue",Broker="",Weightage=3,RowNumber=4},
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Net Revenue",Broker="",Weightage=3,RowNumber=5},
    
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Cost of Revenue",Broker="BW",Weightage=1,RowNumber=6},
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Cost of Revenue",Broker="3P-1",Weightage=1,RowNumber=7},
    
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Cost of Revenue",Broker="",Weightage=2,RowNumber=8},
    
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Cost of Revenue",Broker="",Weightage=3,RowNumber=9},
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Cost of Revenue",Broker="",Weightage=3,RowNumber=10},
    
                    new WeightageRowNumber { Section = "Key Financials", Lineitem = "Quick Ratio",Broker="BW",Weightage=1,RowNumber=11},
                    new WeightageRowNumber { Section = "Key Financials", Lineitem = "Quick Ratio",Broker="3P-1",Weightage=1,RowNumber=12},
    
                    new WeightageRowNumber { Section = "Key Financials", Lineitem = "Quick Ratio",Broker="",Weightage=2,RowNumber=13},
    
                    new WeightageRowNumber { Section = "Key Financials", Lineitem = "Quick Ratio",Broker="",Weightage=3,RowNumber=14},
                    new WeightageRowNumber { Section = "Key Financials", Lineitem = "Quick Ratio",Broker="",Weightage=3,RowNumber=15},
    
                };

    My WeightageRowNumber class look like

    public class WeightageRowNumber
        {
    
            public WeightageRowNumber()
            {
                this.Broker = string.Empty;
                this.Section = string.Empty;
                this.Lineitem = string.Empty;
                this.RowNumber = -1;
                this.Weightage = 0;
                this.Id = "-1";
            }
    
            public string Broker { get; set; }
            public string Section { get; set; }
            public string Lineitem { get; set; }
            public int RowNumber { get; set; }
            public int Weightage { get; set; }
    
            public string Id { get; set; }
    
        }             

    please tell me how to use LINQ to populate my WeightageRowNumberall instead of foreach loop. if possible guide with code. thanks

    Saturday, February 9, 2019 5:15 PM

Answers

All replies

  • A Linq projection can make a anaymous object or concrete object that's the only way I know that Linq is going to make an object in the manner you are talking about when using a Linq query.   

    https://csharp-station.com/Tutorial/Linq/Lesson02

    • Edited by DA924x Sunday, February 10, 2019 6:37 PM
    Saturday, February 9, 2019 5:35 PM
  • Hi Studip_inn,

    Thank you for posting here.

    For your question, you also could use List<T>.ForEach(Action<T>) Method.

    You could download the source file from the code project for reference.

    https://www.codeproject.com/Articles/991610/ForEach-LINQ-Extension

    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 Tuesday, February 12, 2019 6:24 AM
    • Marked as answer by Sudip_inn Wednesday, February 13, 2019 6:05 PM
    Monday, February 11, 2019 8:29 AM
    Moderator