简体   繁体   中英

Copy element of Generic Collection to another Collection in C#

Basically, I am trying to ask the user for the ID number of a product, then copy the element with that number to a new Collection.

I've added the products here.

    //Set up shop and add products
    public void SetUpShop()
    {
        products.Add(new Farm_Shop(1, "Apple", "Fruit\t", 0.49, 40, 'n'));
        products.Add(new Farm_Shop(2, "Orange", "Fruit\t", 0.59, 35, 'n'));
    }

Then, I display a menu which allows the user to type in the ID number they want, for example, "1 for Apple." What I'm then trying to make it do is add all of the information in the "Apple" collection to a new Collection.

//Display all items
    public void DisplayAll()
    {

        Console.WriteLine("\nID\tProduct\tCategory\tPrice\tStock\tIn Basket?");
        foreach (Farm_Shop temp in products)
        {
            //display each product to console by using Display method in Farm Shop class
            temp.Display();
        }

        //Allow user to add a product to the basket (change 'inBasket' to 'y')
        Console.WriteLine("Enter the ID of the item you wish to add to your basket: ");
        int idToAdd = int.Parse(Console.ReadLine());

        //Add array with the same as ID as person wants to add to basket to Basket array.
        basket.Add //Trying to add element to the "basket" collection.
    }

I can obviously add elements to a collection, but it is the fact that I have to get the ID number and then compare that to whats in all of the arrays, then take the array with that ID in it and add it to the new collection.

Help appreciated as I've been trying this for ages now. And I've just realised that I've mixed myself up in switching between saying array and collection in my explanation.

Thanks.

It is better to use Dictionary in your case. Addition and location of the right element will be trivial then.

So, this is initialization:

products = new Dictionary<int, Farm_Shop>
    {
        { 1, new Farm_Shop(1, "Apple", "Fruit\t", 0.49, 40, 'n') },
        { 2, new Farm_Shop(2, "Orange", "Fruit\t", 0.59, 35, 'n') }
    };
basket = new Dictionary<int, Farm_Shop>();

And this is the addition:

int idToAdd = int.Parse(Console.ReadLine());
Farm_Shop selected;
if (products.TryGet(idToAdd, out selected))
{
    basket[idToAdd] = selected;
}

Usage of the indexer (square brackets) ensures you don't get exception in case the item with the same ID is already in the dictionary.

Hope I understood your problem right.

Farm_Shop selectedItem = products.Single(x => x.ID == idToAdd);
selectedItem.InBasket = 'y';
basket.Add(selectedItem);

If you want more efficient lookups, you should make products a dictionary that maps the IDs to the items. Then this first line would become:

Farm_Shop selectedItem = products[idToAdd];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM