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

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

Sudip_inn

Guest
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

Continue reading...
 
Back
Top