Combine subitems from list of items












0















i want to ask you for help with combine all subitems from list, which looks like:



public class Subitem
{
public string Name { get; set; }
public string Code { get; set; }
public float Price { get; set; }
}

public class Item
{
public string Name { get; set; }
public string Code { get; set; }
public List<Subitem> Subitems { get; set; }
}



var components = new List<Item>();
components.Add(new Item()
{
Code = "ItemCode1",
Name = "Item1Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode1", Price = 32 },
new Subitem { Code = "SubitemCode2", Price = 21 },
new Subitem { Code = "SubitemCode3", Price = 11 },
new Subitem { Code = "SubitemCode4", Price = 51 }
}
});
components.Add(new Item()
{
Code = "ItemCode2",
Name = "Item2Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode5", Price = 11 },
new Subitem { Code = "SubitemCode6", Price = 22 },
new Subitem { Code = "SubitemCode7", Price = 52 },
new Subitem { Code = "SubitemCode8", Price = 63 }
}
});
components.Add(new Item()
{
Code = "ItemCode3",
Name = "Item3Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode9", Price = 11 },
new Subitem { Code = "SubitemCode10", Price = 22 },
new Subitem { Code = "SubitemCode11", Price = 52 },
new Subitem { Code = "SubitemCode12", Price = 63 }
}
});
components.Add(new Item()
{
Code = "ItemCode4",
Name = "Item4Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode13", Price = 11 },
new Subitem { Code = "SubitemCode14", Price = 22 },
new Subitem { Code = "SubitemCode15", Price = 52 },
new Subitem { Code = "SubitemCode16", Price = 63 }
}
});


I want to combine all subitems in model which looks like this:



new { Code = SubitemCode1, Price = 32 }
...
new { Code = SubitemCode8, Price = 63 }


new { Code = "SubitemCode1:SubitemCode5", Price = 43 } //11 + 32
...
new { Code = "SubitemCode1:SubitemCode8", Price = 95 } //32 + 63


new { Code = "SubitemCode2:SubitemCode5", Price = ... }
...
new { Code = "SubitemCode2:SubitemCode8", Price = ... }

@EDIT

new { Code = "SubitemCode1:SubitemCode5:SubitemCode9", Price = 54 } // 11 + 32 + 11
...
new { Code = "SubitemCode1:SubitemCode5:SubitemCode12", Price = 96 } // 11 + 32 + 63

new { Code = "SubitemCode1:SubitemCode6:SubitemCode9", Price = ... }
...
new { Code = "SubitemCode1:SubitemCode6:SubitemCode12", Price = ... }
...
new { Code = "SubitemCode1:SubitemCode8:SubitemCode9", Price = ... }
...
new { Code = "SubitemCode1:SubitemCode8:SubitemCode12", Price = ... }

new { Code = "SubitemCode2:SubitemCode5:SubitemCode9", Price = ... }
...


Can anyone explain to me how to get on with it? There could be 1-5 Item and 1-10 in Subitems in each Item and i need to have all combination of subitems with the addition of prices.



Subitems from Item in which they are, is not combinable, only Subitems from other Item



Thank You in Advance,



Best Regards.










share|improve this question

























  • Can you share your attempt to do this?

    – Reniuz
    Nov 20 '18 at 11:09











  • I don't even know how to start with it. I have idea to implement foreach in foreach (item1, subitem1, item2, subitem2,...) but the count of Item in list is not const. I need some hints on how to do it. Thanks!

    – Mateusz
    Nov 20 '18 at 11:35
















0















i want to ask you for help with combine all subitems from list, which looks like:



public class Subitem
{
public string Name { get; set; }
public string Code { get; set; }
public float Price { get; set; }
}

public class Item
{
public string Name { get; set; }
public string Code { get; set; }
public List<Subitem> Subitems { get; set; }
}



var components = new List<Item>();
components.Add(new Item()
{
Code = "ItemCode1",
Name = "Item1Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode1", Price = 32 },
new Subitem { Code = "SubitemCode2", Price = 21 },
new Subitem { Code = "SubitemCode3", Price = 11 },
new Subitem { Code = "SubitemCode4", Price = 51 }
}
});
components.Add(new Item()
{
Code = "ItemCode2",
Name = "Item2Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode5", Price = 11 },
new Subitem { Code = "SubitemCode6", Price = 22 },
new Subitem { Code = "SubitemCode7", Price = 52 },
new Subitem { Code = "SubitemCode8", Price = 63 }
}
});
components.Add(new Item()
{
Code = "ItemCode3",
Name = "Item3Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode9", Price = 11 },
new Subitem { Code = "SubitemCode10", Price = 22 },
new Subitem { Code = "SubitemCode11", Price = 52 },
new Subitem { Code = "SubitemCode12", Price = 63 }
}
});
components.Add(new Item()
{
Code = "ItemCode4",
Name = "Item4Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode13", Price = 11 },
new Subitem { Code = "SubitemCode14", Price = 22 },
new Subitem { Code = "SubitemCode15", Price = 52 },
new Subitem { Code = "SubitemCode16", Price = 63 }
}
});


I want to combine all subitems in model which looks like this:



new { Code = SubitemCode1, Price = 32 }
...
new { Code = SubitemCode8, Price = 63 }


new { Code = "SubitemCode1:SubitemCode5", Price = 43 } //11 + 32
...
new { Code = "SubitemCode1:SubitemCode8", Price = 95 } //32 + 63


new { Code = "SubitemCode2:SubitemCode5", Price = ... }
...
new { Code = "SubitemCode2:SubitemCode8", Price = ... }

@EDIT

new { Code = "SubitemCode1:SubitemCode5:SubitemCode9", Price = 54 } // 11 + 32 + 11
...
new { Code = "SubitemCode1:SubitemCode5:SubitemCode12", Price = 96 } // 11 + 32 + 63

new { Code = "SubitemCode1:SubitemCode6:SubitemCode9", Price = ... }
...
new { Code = "SubitemCode1:SubitemCode6:SubitemCode12", Price = ... }
...
new { Code = "SubitemCode1:SubitemCode8:SubitemCode9", Price = ... }
...
new { Code = "SubitemCode1:SubitemCode8:SubitemCode12", Price = ... }

new { Code = "SubitemCode2:SubitemCode5:SubitemCode9", Price = ... }
...


Can anyone explain to me how to get on with it? There could be 1-5 Item and 1-10 in Subitems in each Item and i need to have all combination of subitems with the addition of prices.



Subitems from Item in which they are, is not combinable, only Subitems from other Item



Thank You in Advance,



Best Regards.










share|improve this question

























  • Can you share your attempt to do this?

    – Reniuz
    Nov 20 '18 at 11:09











  • I don't even know how to start with it. I have idea to implement foreach in foreach (item1, subitem1, item2, subitem2,...) but the count of Item in list is not const. I need some hints on how to do it. Thanks!

    – Mateusz
    Nov 20 '18 at 11:35














0












0








0








i want to ask you for help with combine all subitems from list, which looks like:



public class Subitem
{
public string Name { get; set; }
public string Code { get; set; }
public float Price { get; set; }
}

public class Item
{
public string Name { get; set; }
public string Code { get; set; }
public List<Subitem> Subitems { get; set; }
}



var components = new List<Item>();
components.Add(new Item()
{
Code = "ItemCode1",
Name = "Item1Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode1", Price = 32 },
new Subitem { Code = "SubitemCode2", Price = 21 },
new Subitem { Code = "SubitemCode3", Price = 11 },
new Subitem { Code = "SubitemCode4", Price = 51 }
}
});
components.Add(new Item()
{
Code = "ItemCode2",
Name = "Item2Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode5", Price = 11 },
new Subitem { Code = "SubitemCode6", Price = 22 },
new Subitem { Code = "SubitemCode7", Price = 52 },
new Subitem { Code = "SubitemCode8", Price = 63 }
}
});
components.Add(new Item()
{
Code = "ItemCode3",
Name = "Item3Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode9", Price = 11 },
new Subitem { Code = "SubitemCode10", Price = 22 },
new Subitem { Code = "SubitemCode11", Price = 52 },
new Subitem { Code = "SubitemCode12", Price = 63 }
}
});
components.Add(new Item()
{
Code = "ItemCode4",
Name = "Item4Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode13", Price = 11 },
new Subitem { Code = "SubitemCode14", Price = 22 },
new Subitem { Code = "SubitemCode15", Price = 52 },
new Subitem { Code = "SubitemCode16", Price = 63 }
}
});


I want to combine all subitems in model which looks like this:



new { Code = SubitemCode1, Price = 32 }
...
new { Code = SubitemCode8, Price = 63 }


new { Code = "SubitemCode1:SubitemCode5", Price = 43 } //11 + 32
...
new { Code = "SubitemCode1:SubitemCode8", Price = 95 } //32 + 63


new { Code = "SubitemCode2:SubitemCode5", Price = ... }
...
new { Code = "SubitemCode2:SubitemCode8", Price = ... }

@EDIT

new { Code = "SubitemCode1:SubitemCode5:SubitemCode9", Price = 54 } // 11 + 32 + 11
...
new { Code = "SubitemCode1:SubitemCode5:SubitemCode12", Price = 96 } // 11 + 32 + 63

new { Code = "SubitemCode1:SubitemCode6:SubitemCode9", Price = ... }
...
new { Code = "SubitemCode1:SubitemCode6:SubitemCode12", Price = ... }
...
new { Code = "SubitemCode1:SubitemCode8:SubitemCode9", Price = ... }
...
new { Code = "SubitemCode1:SubitemCode8:SubitemCode12", Price = ... }

new { Code = "SubitemCode2:SubitemCode5:SubitemCode9", Price = ... }
...


Can anyone explain to me how to get on with it? There could be 1-5 Item and 1-10 in Subitems in each Item and i need to have all combination of subitems with the addition of prices.



Subitems from Item in which they are, is not combinable, only Subitems from other Item



Thank You in Advance,



Best Regards.










share|improve this question
















i want to ask you for help with combine all subitems from list, which looks like:



public class Subitem
{
public string Name { get; set; }
public string Code { get; set; }
public float Price { get; set; }
}

public class Item
{
public string Name { get; set; }
public string Code { get; set; }
public List<Subitem> Subitems { get; set; }
}



var components = new List<Item>();
components.Add(new Item()
{
Code = "ItemCode1",
Name = "Item1Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode1", Price = 32 },
new Subitem { Code = "SubitemCode2", Price = 21 },
new Subitem { Code = "SubitemCode3", Price = 11 },
new Subitem { Code = "SubitemCode4", Price = 51 }
}
});
components.Add(new Item()
{
Code = "ItemCode2",
Name = "Item2Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode5", Price = 11 },
new Subitem { Code = "SubitemCode6", Price = 22 },
new Subitem { Code = "SubitemCode7", Price = 52 },
new Subitem { Code = "SubitemCode8", Price = 63 }
}
});
components.Add(new Item()
{
Code = "ItemCode3",
Name = "Item3Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode9", Price = 11 },
new Subitem { Code = "SubitemCode10", Price = 22 },
new Subitem { Code = "SubitemCode11", Price = 52 },
new Subitem { Code = "SubitemCode12", Price = 63 }
}
});
components.Add(new Item()
{
Code = "ItemCode4",
Name = "Item4Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode13", Price = 11 },
new Subitem { Code = "SubitemCode14", Price = 22 },
new Subitem { Code = "SubitemCode15", Price = 52 },
new Subitem { Code = "SubitemCode16", Price = 63 }
}
});


I want to combine all subitems in model which looks like this:



new { Code = SubitemCode1, Price = 32 }
...
new { Code = SubitemCode8, Price = 63 }


new { Code = "SubitemCode1:SubitemCode5", Price = 43 } //11 + 32
...
new { Code = "SubitemCode1:SubitemCode8", Price = 95 } //32 + 63


new { Code = "SubitemCode2:SubitemCode5", Price = ... }
...
new { Code = "SubitemCode2:SubitemCode8", Price = ... }

@EDIT

new { Code = "SubitemCode1:SubitemCode5:SubitemCode9", Price = 54 } // 11 + 32 + 11
...
new { Code = "SubitemCode1:SubitemCode5:SubitemCode12", Price = 96 } // 11 + 32 + 63

new { Code = "SubitemCode1:SubitemCode6:SubitemCode9", Price = ... }
...
new { Code = "SubitemCode1:SubitemCode6:SubitemCode12", Price = ... }
...
new { Code = "SubitemCode1:SubitemCode8:SubitemCode9", Price = ... }
...
new { Code = "SubitemCode1:SubitemCode8:SubitemCode12", Price = ... }

new { Code = "SubitemCode2:SubitemCode5:SubitemCode9", Price = ... }
...


Can anyone explain to me how to get on with it? There could be 1-5 Item and 1-10 in Subitems in each Item and i need to have all combination of subitems with the addition of prices.



Subitems from Item in which they are, is not combinable, only Subitems from other Item



Thank You in Advance,



Best Regards.







c# algorithm combinations






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 11:31







Mateusz

















asked Nov 20 '18 at 11:01









MateuszMateusz

56110




56110













  • Can you share your attempt to do this?

    – Reniuz
    Nov 20 '18 at 11:09











  • I don't even know how to start with it. I have idea to implement foreach in foreach (item1, subitem1, item2, subitem2,...) but the count of Item in list is not const. I need some hints on how to do it. Thanks!

    – Mateusz
    Nov 20 '18 at 11:35



















  • Can you share your attempt to do this?

    – Reniuz
    Nov 20 '18 at 11:09











  • I don't even know how to start with it. I have idea to implement foreach in foreach (item1, subitem1, item2, subitem2,...) but the count of Item in list is not const. I need some hints on how to do it. Thanks!

    – Mateusz
    Nov 20 '18 at 11:35

















Can you share your attempt to do this?

– Reniuz
Nov 20 '18 at 11:09





Can you share your attempt to do this?

– Reniuz
Nov 20 '18 at 11:09













I don't even know how to start with it. I have idea to implement foreach in foreach (item1, subitem1, item2, subitem2,...) but the count of Item in list is not const. I need some hints on how to do it. Thanks!

– Mateusz
Nov 20 '18 at 11:35





I don't even know how to start with it. I have idea to implement foreach in foreach (item1, subitem1, item2, subitem2,...) but the count of Item in list is not const. I need some hints on how to do it. Thanks!

– Mateusz
Nov 20 '18 at 11:35












2 Answers
2






active

oldest

votes


















1














You can simply make join over two different list like



var result = components[0].Subitems
.Join(components[1].Subitems, x => true, y => true, (a, b) => new { Code = a.Code + ":" + b.Code, Price = a.Price + b.Price })
.ToList();


OR you can do this by using linq



var result = from a in components[0].Subitems
from b in components[1].Subitems
select new
{
Code = a.Code + ":" + b.Code,
Price = a.Price + b.Price
};


And finally print the result



foreach (var item in result)
{
Console.WriteLine("Code: " + item.Code + "t Price: " + item.Price);
}


Output:



enter image description here






share|improve this answer


























  • Thanks for answer but count of item in List is not const. There could be more than two Item in List<Item>, then your code will won't work

    – Mateusz
    Nov 20 '18 at 11:23











  • Of course, post has been modified.

    – Mateusz
    Nov 20 '18 at 11:33











  • I'll update my code soon...

    – er-mfahhgk
    Nov 20 '18 at 16:32



















0














Okay, i think that i got this.



The code looks like:



var result = new List<SubItem>(); //list of combined SubItems

var ms = 0; // start index of items from list to combine
var mk = 0; // end index of items from list to combine

for (int i = 0; i < components.Count; i++) // count of all items
{
if (i == 0) //if there is a first item then we don't combine codes and prices
{
for (int mat = 0; mat < components[i].SubItems.Count; mat++)
{
var data = components[i].SubItem[mat];
result.Add(new SubItem { Price = data.Price, Code = data.Code });
mk = mat; // set last index of SubItem to combine
}
continue;
}

for (int j = ms; j < mk + 1; j++) // iterate from first to last SubItem to combine them with new SubItems
{
for (int mat = 0; mat < components[i].SubItems.Count; mat++) // iterate through SubItems
{
result.Add(new SubItem { Code = result[j].Code + ":" + components[i].SubItem[mat].Code, price = result[j].Price + components[i].SubItem[mat].Price }); // Combine last SubItem with now iterating Subitem.
}
}

ms = mk + 1; // update new start index to combine
mk = result.Count - 1; // update new end index to combine
}


I achieved what I wanted. :)



Best Regards!






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391552%2fcombine-subitems-from-list-of-items%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can simply make join over two different list like



    var result = components[0].Subitems
    .Join(components[1].Subitems, x => true, y => true, (a, b) => new { Code = a.Code + ":" + b.Code, Price = a.Price + b.Price })
    .ToList();


    OR you can do this by using linq



    var result = from a in components[0].Subitems
    from b in components[1].Subitems
    select new
    {
    Code = a.Code + ":" + b.Code,
    Price = a.Price + b.Price
    };


    And finally print the result



    foreach (var item in result)
    {
    Console.WriteLine("Code: " + item.Code + "t Price: " + item.Price);
    }


    Output:



    enter image description here






    share|improve this answer


























    • Thanks for answer but count of item in List is not const. There could be more than two Item in List<Item>, then your code will won't work

      – Mateusz
      Nov 20 '18 at 11:23











    • Of course, post has been modified.

      – Mateusz
      Nov 20 '18 at 11:33











    • I'll update my code soon...

      – er-mfahhgk
      Nov 20 '18 at 16:32
















    1














    You can simply make join over two different list like



    var result = components[0].Subitems
    .Join(components[1].Subitems, x => true, y => true, (a, b) => new { Code = a.Code + ":" + b.Code, Price = a.Price + b.Price })
    .ToList();


    OR you can do this by using linq



    var result = from a in components[0].Subitems
    from b in components[1].Subitems
    select new
    {
    Code = a.Code + ":" + b.Code,
    Price = a.Price + b.Price
    };


    And finally print the result



    foreach (var item in result)
    {
    Console.WriteLine("Code: " + item.Code + "t Price: " + item.Price);
    }


    Output:



    enter image description here






    share|improve this answer


























    • Thanks for answer but count of item in List is not const. There could be more than two Item in List<Item>, then your code will won't work

      – Mateusz
      Nov 20 '18 at 11:23











    • Of course, post has been modified.

      – Mateusz
      Nov 20 '18 at 11:33











    • I'll update my code soon...

      – er-mfahhgk
      Nov 20 '18 at 16:32














    1












    1








    1







    You can simply make join over two different list like



    var result = components[0].Subitems
    .Join(components[1].Subitems, x => true, y => true, (a, b) => new { Code = a.Code + ":" + b.Code, Price = a.Price + b.Price })
    .ToList();


    OR you can do this by using linq



    var result = from a in components[0].Subitems
    from b in components[1].Subitems
    select new
    {
    Code = a.Code + ":" + b.Code,
    Price = a.Price + b.Price
    };


    And finally print the result



    foreach (var item in result)
    {
    Console.WriteLine("Code: " + item.Code + "t Price: " + item.Price);
    }


    Output:



    enter image description here






    share|improve this answer















    You can simply make join over two different list like



    var result = components[0].Subitems
    .Join(components[1].Subitems, x => true, y => true, (a, b) => new { Code = a.Code + ":" + b.Code, Price = a.Price + b.Price })
    .ToList();


    OR you can do this by using linq



    var result = from a in components[0].Subitems
    from b in components[1].Subitems
    select new
    {
    Code = a.Code + ":" + b.Code,
    Price = a.Price + b.Price
    };


    And finally print the result



    foreach (var item in result)
    {
    Console.WriteLine("Code: " + item.Code + "t Price: " + item.Price);
    }


    Output:



    enter image description here







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 '18 at 11:18

























    answered Nov 20 '18 at 11:10









    er-mfahhgker-mfahhgk

    5,3992616




    5,3992616













    • Thanks for answer but count of item in List is not const. There could be more than two Item in List<Item>, then your code will won't work

      – Mateusz
      Nov 20 '18 at 11:23











    • Of course, post has been modified.

      – Mateusz
      Nov 20 '18 at 11:33











    • I'll update my code soon...

      – er-mfahhgk
      Nov 20 '18 at 16:32



















    • Thanks for answer but count of item in List is not const. There could be more than two Item in List<Item>, then your code will won't work

      – Mateusz
      Nov 20 '18 at 11:23











    • Of course, post has been modified.

      – Mateusz
      Nov 20 '18 at 11:33











    • I'll update my code soon...

      – er-mfahhgk
      Nov 20 '18 at 16:32

















    Thanks for answer but count of item in List is not const. There could be more than two Item in List<Item>, then your code will won't work

    – Mateusz
    Nov 20 '18 at 11:23





    Thanks for answer but count of item in List is not const. There could be more than two Item in List<Item>, then your code will won't work

    – Mateusz
    Nov 20 '18 at 11:23













    Of course, post has been modified.

    – Mateusz
    Nov 20 '18 at 11:33





    Of course, post has been modified.

    – Mateusz
    Nov 20 '18 at 11:33













    I'll update my code soon...

    – er-mfahhgk
    Nov 20 '18 at 16:32





    I'll update my code soon...

    – er-mfahhgk
    Nov 20 '18 at 16:32













    0














    Okay, i think that i got this.



    The code looks like:



    var result = new List<SubItem>(); //list of combined SubItems

    var ms = 0; // start index of items from list to combine
    var mk = 0; // end index of items from list to combine

    for (int i = 0; i < components.Count; i++) // count of all items
    {
    if (i == 0) //if there is a first item then we don't combine codes and prices
    {
    for (int mat = 0; mat < components[i].SubItems.Count; mat++)
    {
    var data = components[i].SubItem[mat];
    result.Add(new SubItem { Price = data.Price, Code = data.Code });
    mk = mat; // set last index of SubItem to combine
    }
    continue;
    }

    for (int j = ms; j < mk + 1; j++) // iterate from first to last SubItem to combine them with new SubItems
    {
    for (int mat = 0; mat < components[i].SubItems.Count; mat++) // iterate through SubItems
    {
    result.Add(new SubItem { Code = result[j].Code + ":" + components[i].SubItem[mat].Code, price = result[j].Price + components[i].SubItem[mat].Price }); // Combine last SubItem with now iterating Subitem.
    }
    }

    ms = mk + 1; // update new start index to combine
    mk = result.Count - 1; // update new end index to combine
    }


    I achieved what I wanted. :)



    Best Regards!






    share|improve this answer




























      0














      Okay, i think that i got this.



      The code looks like:



      var result = new List<SubItem>(); //list of combined SubItems

      var ms = 0; // start index of items from list to combine
      var mk = 0; // end index of items from list to combine

      for (int i = 0; i < components.Count; i++) // count of all items
      {
      if (i == 0) //if there is a first item then we don't combine codes and prices
      {
      for (int mat = 0; mat < components[i].SubItems.Count; mat++)
      {
      var data = components[i].SubItem[mat];
      result.Add(new SubItem { Price = data.Price, Code = data.Code });
      mk = mat; // set last index of SubItem to combine
      }
      continue;
      }

      for (int j = ms; j < mk + 1; j++) // iterate from first to last SubItem to combine them with new SubItems
      {
      for (int mat = 0; mat < components[i].SubItems.Count; mat++) // iterate through SubItems
      {
      result.Add(new SubItem { Code = result[j].Code + ":" + components[i].SubItem[mat].Code, price = result[j].Price + components[i].SubItem[mat].Price }); // Combine last SubItem with now iterating Subitem.
      }
      }

      ms = mk + 1; // update new start index to combine
      mk = result.Count - 1; // update new end index to combine
      }


      I achieved what I wanted. :)



      Best Regards!






      share|improve this answer


























        0












        0








        0







        Okay, i think that i got this.



        The code looks like:



        var result = new List<SubItem>(); //list of combined SubItems

        var ms = 0; // start index of items from list to combine
        var mk = 0; // end index of items from list to combine

        for (int i = 0; i < components.Count; i++) // count of all items
        {
        if (i == 0) //if there is a first item then we don't combine codes and prices
        {
        for (int mat = 0; mat < components[i].SubItems.Count; mat++)
        {
        var data = components[i].SubItem[mat];
        result.Add(new SubItem { Price = data.Price, Code = data.Code });
        mk = mat; // set last index of SubItem to combine
        }
        continue;
        }

        for (int j = ms; j < mk + 1; j++) // iterate from first to last SubItem to combine them with new SubItems
        {
        for (int mat = 0; mat < components[i].SubItems.Count; mat++) // iterate through SubItems
        {
        result.Add(new SubItem { Code = result[j].Code + ":" + components[i].SubItem[mat].Code, price = result[j].Price + components[i].SubItem[mat].Price }); // Combine last SubItem with now iterating Subitem.
        }
        }

        ms = mk + 1; // update new start index to combine
        mk = result.Count - 1; // update new end index to combine
        }


        I achieved what I wanted. :)



        Best Regards!






        share|improve this answer













        Okay, i think that i got this.



        The code looks like:



        var result = new List<SubItem>(); //list of combined SubItems

        var ms = 0; // start index of items from list to combine
        var mk = 0; // end index of items from list to combine

        for (int i = 0; i < components.Count; i++) // count of all items
        {
        if (i == 0) //if there is a first item then we don't combine codes and prices
        {
        for (int mat = 0; mat < components[i].SubItems.Count; mat++)
        {
        var data = components[i].SubItem[mat];
        result.Add(new SubItem { Price = data.Price, Code = data.Code });
        mk = mat; // set last index of SubItem to combine
        }
        continue;
        }

        for (int j = ms; j < mk + 1; j++) // iterate from first to last SubItem to combine them with new SubItems
        {
        for (int mat = 0; mat < components[i].SubItems.Count; mat++) // iterate through SubItems
        {
        result.Add(new SubItem { Code = result[j].Code + ":" + components[i].SubItem[mat].Code, price = result[j].Price + components[i].SubItem[mat].Price }); // Combine last SubItem with now iterating Subitem.
        }
        }

        ms = mk + 1; // update new start index to combine
        mk = result.Count - 1; // update new end index to combine
        }


        I achieved what I wanted. :)



        Best Regards!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 10:05









        MateuszMateusz

        56110




        56110






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391552%2fcombine-subitems-from-list-of-items%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith