Click here to Skip to main content
15,905,068 members
Home / Discussions / C#
   

C#

 
Questionlinq to objects Extract values between start and end strings Pin
Mou_kol1-May-21 3:09
Mou_kol1-May-21 3:09 
AnswerRe: linq to objects Extract values between start and end strings Pin
OriginalGriff1-May-21 4:28
mveOriginalGriff1-May-21 4:28 
AnswerRe: linq to objects Extract values between start and end strings Pin
Richard MacCutchan1-May-21 4:41
mveRichard MacCutchan1-May-21 4:41 
GeneralRe: linq to objects Extract values between start and end strings Pin
Mou_kol1-May-21 6:44
Mou_kol1-May-21 6:44 
GeneralRe: linq to objects Extract values between start and end strings Pin
Richard MacCutchan1-May-21 6:48
mveRichard MacCutchan1-May-21 6:48 
AnswerRe: linq to objects Extract values between start and end strings Pin
Gerry Schmitz1-May-21 7:16
mveGerry Schmitz1-May-21 7:16 
AnswerRe: linq to objects Extract values between start and end strings Pin
Mycroft Holmes1-May-21 11:54
professionalMycroft Holmes1-May-21 11:54 
GeneralRe: linq to objects Extract values between start and end strings Pin
Mou_kol1-May-21 21:18
Mou_kol1-May-21 21:18 
GeneralRe: linq to objects Extract values between start and end strings Pin
Mycroft Holmes2-May-21 12:21
professionalMycroft Holmes2-May-21 12:21 
AnswerRe: linq to objects Extract values between start and end strings Pin
Eddy Vluggen3-May-21 6:24
professionalEddy Vluggen3-May-21 6:24 
QuestionHow to report progress from Async function Pin
Mou_kol29-Apr-21 20:23
Mou_kol29-Apr-21 20:23 
AnswerRe: How to report progress from Async function Pin
Richard Deeming29-Apr-21 21:15
mveRichard Deeming29-Apr-21 21:15 
GeneralRe: How to report progress from Async function Pin
Mou_kol29-Apr-21 22:10
Mou_kol29-Apr-21 22:10 
GeneralRe: How to report progress from Async function Pin
Richard Deeming30-Apr-21 1:45
mveRichard Deeming30-Apr-21 1:45 
GeneralRe: How to report progress from Async function Pin
Mou_kol1-May-21 3:18
Mou_kol1-May-21 3:18 
GeneralRe: How to report progress from Async function Pin
Dave Kreskowiak1-May-21 5:29
mveDave Kreskowiak1-May-21 5:29 
AnswerRe: How to report progress from Async function Pin
OriginalGriff30-Apr-21 19:56
mveOriginalGriff30-Apr-21 19:56 
GeneralRe: How to report progress from Async function Pin
Mou_kol1-May-21 3:15
Mou_kol1-May-21 3:15 
GeneralRe: How to report progress from Async function Pin
OriginalGriff1-May-21 4:21
mveOriginalGriff1-May-21 4:21 
GeneralRe: How to report progress from Async function Pin
Mou_kol1-May-21 6:42
Mou_kol1-May-21 6:42 
AnswerRe: How to report progress from Async function Pin
Eddy Vluggen3-May-21 6:25
professionalEddy Vluggen3-May-21 6:25 
QuestionC# How to speed up for loop with large data iteration Pin
Mou_kol29-Apr-21 2:40
Mou_kol29-Apr-21 2:40 
I am iterating in 2509693 data in for loop which is taking long time. i am using VS2013 and .Net v4.5.2

so see my code and suggest some approach or changes for my existing code which speed up execution of my for loop.

This ds.Tables[1] has 2509693 data please guide me how to restructure & speed up below code. Thanks
C#
public static List<ElementHierarchy> GetElementHierarchy(DataSet ds)
 {
     List<ElementHierarchy> _ElmHierarchy = new List<ElementHierarchy>();
    
     string StrPrevDisplayInCSM = "", DisplayInCSM = "", Section = "", LineItem = "", LastGroupName = "", BGColor="",
         BlueMatrix1stElementFormulaText = "", Type = "", Period = "", EarningsType = "", ParentGroup = "", HeadingSubheading = "", Box="";
     int row = 6, EarningID = 0, LineItemID = 0, BMID = 0, ID = 0, ParentID=0;
     bool IsNextElementGroup = false;
     List<ListOfSection> lstData = new List<ListOfSection>();
     bool IsGreenHeader = false;
    
     for (int p = 0; p <= ds.Tables[1].Rows.Count - 1; p++)
     {
         ID = Convert.ToInt32(ds.Tables[1].Rows[p]["ID"].ToString());
         ParentID = Convert.ToInt32(ds.Tables[1].Rows[p]["ParentID"].ToString());
            
         EarningID = 0; 
         Section = (ds.Tables[1].Rows[p]["Section"] == DBNull.Value ? "" : ds.Tables[1].Rows[p]["Section"].ToString());
         LineItem = (ds.Tables[1].Rows[p]["LineItem"] == DBNull.Value ? "" : ds.Tables[1].Rows[p]["LineItem"].ToString());
         DisplayInCSM = ds.Tables[1].Rows[p]["DisplayInCSM"].ToString();
         Type = ds.Tables[1].Rows[p]["Type"].ToString();
         BlueMatrix1stElementFormulaText = (ds.Tables[1].Rows[p]["BlueMatrix1stElementFormulaText"] == null 
             ? "" : ds.Tables[1].Rows[p]["BlueMatrix1stElementFormulaText"].ToString());
         Period = (ds.Tables[1].Rows[p]["Period"] == DBNull.Value ? "" : ds.Tables[1].Rows[p]["Period"].ToString());
         HeadingSubheading = (ds.Tables[1].Rows[p]["HeadingSubheading"] == null ? "" : ds.Tables[1].Rows[p]["HeadingSubheading"].ToString());
         Box = (ds.Tables[1].Rows[p]["Box"] == DBNull.Value ? "" : ds.Tables[1].Rows[p]["Box"].ToString());
         LineItemID = Convert.ToInt32(ds.Tables[1].Rows[p]["LineItemID"].ToString());
         BMID = Convert.ToInt16(ds.Tables[1].Rows[p]["BMID"].ToString());
         BGColor = (ds.Tables[1].Rows[p]["BGColor"] == null ? "" : ds.Tables[1].Rows[p]["BGColor"].ToString());
    
    
         if (BGColor.Contains("ff003300"))
         {
             IsGreenHeader = true;
         }
         else
         {
             IsGreenHeader = false;
         }
    
         if (StrPrevDisplayInCSM != "" && StrPrevDisplayInCSM != DisplayInCSM && (Type == "LINEITEM" || Type=="BM"))
         {
             row++;
         }
    
         if (Type == "GROUP")
         {
             if (IsNextElementGroup)
             {
                 row++;
             }
             else if (p > 0 && !IsNextElementGroup)
             {
                 row++;
    
                 if (p > 0 && HeadingSubheading=="H")
                 {
                     row++;
                 }
    
                 if (p > 0 && HeadingSubheading == "S")
                 {
                     row++;
                 }
             }
             else if (p > 0 && IsGreenHeader)
             {
                 row++;
             }
             else if (p > 0 && ds.Tables[1].AsEnumerable().Any(a => a.Field<int>("ParentID") == ID && a.Field<string>("Type") == "GROUP"))
             {
                 row++;
             }
    
             ParentGroup = DisplayInCSM;
    
             if (HeadingSubheading != "")
             {
                 if (HeadingSubheading == "H")
                 {
                     if (Box != "Y")
                     {
    
                     }
                 }
             }
    
             if(IsGreenHeader)
             {
                 row++;
             }
             else if (ds.Tables[1].AsEnumerable().Any(a => a.Field<int>("ParentID") == ID && a.Field<string>("Type")=="GROUP"))
             {
                 row++;
             }
             IsNextElementGroup = true;
    
         }
         else if (Type == "LINEITEM")
         {
             if (!lstData.Any(a =>
                 a.Section == Section
                 && a.LineItem == LineItem
                 && a.Parent == ParentGroup
                 && a.DisplayINCSM == DisplayInCSM
                 && a.EarningsID == EarningID
                 && a.EarningsType == EarningsType
                 && a.Period == Period
                 ))
             {
    
                 if (!_ElmHierarchy.Any(z => z.RowIndex == row))
                 {
                     _ElmHierarchy.Add(new ElementHierarchy
                     {
                         ID=ID,
                         ParentID=ParentID,
                         RowIndex = row,
                         Section = Section,
                         Lineitem = LineItem,
                         Type = "LI",
                         DisplayInCSM = DisplayInCSM,
                         BMFormula = "",
                         LineitemID = LineItemID,
                         BMID = 0
                     });
                 }
    
                 lstData.Add(new ListOfSection
                 {
                     Section = Section,
                     LineItem = LineItem,
                     DisplayINCSM = DisplayInCSM,
                     Parent = ParentGroup,
                     EarningsID = EarningID,
                     EarningsType = EarningsType,
                     Period = Period
                 });
             }
             IsNextElementGroup = false;
             IsGreenHeader = false;
         }
         else if (Type == "BM")
         {
             IsNextElementGroup = false;
             IsGreenHeader = false;
    
             if (!lstData.Any(a =>
                 a.Section == Section
                 && a.LineItem == LineItem
                 && a.Parent == ParentGroup
                 && a.DisplayINCSM == DisplayInCSM
                 && a.EarningsID == EarningID
                 && a.EarningsType == EarningsType
                 && a.Period == Period
                 ))
             {
                 if (!_ElmHierarchy.Any(z => z.RowIndex == row))
                 {
                     _ElmHierarchy.Add(new ElementHierarchy
                     {
                         ID = ID,
                         ParentID = ParentID,
                         RowIndex = row,
                         Section = Section,
                         Lineitem = LineItem,
                         Type = "BM",
                         DisplayInCSM = DisplayInCSM,
                         BMFormula = BlueMatrix1stElementFormulaText,
                         LineitemID = 0,
                         BMID = BMID
                     });
                 }
    
                 lstData.Add(new ListOfSection
                 {
                     Section = Section,
                     LineItem = LineItem,
                     DisplayINCSM = DisplayInCSM,
                     Parent = ParentGroup,
                     EarningsID = EarningID,
                     EarningsType = EarningsType,
                     Period = Period
                 });
             }
         }
    
         StrPrevDisplayInCSM = DisplayInCSM;
     }
     return _ElmHierarchy;
 }
    
    
 public class ListOfSection
 {
     public string Parent { get; set; }
     public int EarningsID { get; set; }
     public string EarningsType { get; set; }
     public string Section { get; set; }
     public string LineItem { get; set; }
     public string DisplayINCSM { get; set; }
     public string Period { get; set; }
 }

AnswerRe: C# How to speed up for loop with large data iteration Pin
Richard Andrew x6429-Apr-21 2:46
professionalRichard Andrew x6429-Apr-21 2:46 
AnswerRe: C# How to speed up for loop with large data iteration Pin
Richard MacCutchan29-Apr-21 3:07
mveRichard MacCutchan29-Apr-21 3:07 
AnswerRe: C# How to speed up for loop with large data iteration Pin
OriginalGriff29-Apr-21 4:16
mveOriginalGriff29-Apr-21 4:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.