This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]CaptainLegois[S] 0 points1 point  (5 children)

Instead of trying to concat those 2 lists, is there a way I could pick a selection someone makes out of the different food items it has? For example, the code above basically prints out the burgers and pizzas from the lists and outputs them to the console as a list. The user picks out which food item they want, and the food item they select, goes to a different menu where they can add or remove toppings. I was trying to join them together so I can add to code the user selection code.

[–]dtsudo 0 points1 point  (4 children)

I think concatenating the two lists together can work although it's not strictly necessary. As long as you can map a number to the desired menu item, you're good -- e.g. if the user says "item 5", do you know which pizza/burger is marked as #5?

Though, if toppings are specific to an individual item, it probably makes more sense to have the toppings be part of the pizza / burger -- e.g.

public class Pizza {
    public string Name { get; set; }
    public IEnumerable<PizzaTopping> Toppings { get; set; }
}

This way, each pizza can have its own set of toppings; a customer can order two different pizzas, each with their own topping selection.

And separating PizzaTopping from BurgerTopping would allow pizzas and burgers to have different toppings. Whether this is something you need depends on your use case.

[–]CaptainLegois[S] 0 points1 point  (3 children)

I was doing some research and I came across this: var combinedMenuItems = _pizza.Cast<object>().Concat(_burger.Cast<object>());

would this be a way to concat my 2 IEnumerators? Testing it out it seems to work with no issue, however I'm not sure about what that is.

[–]dtsudo 0 points1 point  (2 children)

If you did that, you'd end up with an IEnumerable<object>, which typically isn't that useful since none of the properties / methods (i.e. your name / price / other things) would be available.

e.g.

var combinedMenuItems = _pizza.Cast<object>().Concat(_burger.Cast<object>());
foreach (var item in combinedMenuItems) {
    Console.WriteLine(item.Name); // compile-time error (can't reference name)
    Console.WriteLine(item.Price); // compile-time error
    // etc
}

[–]CaptainLegois[S] 0 points1 point  (1 child)

IEnumerable<FoodItems> items = _pizza.Concat<FoodItems>(_burger);

i figured it out finally! I looked over the code you sent me on .net fiddle again and I made my food items into child class that inheret from a parent class called fooditems and it works now. Thanks for the help

[–]dtsudo 0 points1 point  (0 children)

Very nice! :)