Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Given the following segment of code, develop the functionality desired at the TODO comment :
C#
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ObservableCollection<MyItems> _list1 = new ObservableCollection<MyItems>();
            _list1.Add(new MyItems("A001", "Deodorant", 10));
            _list1.Add(new MyItems("A0ZZ", "Hairspray", 20));
            _list1.Add(new MyItems("A002", "Toothpaste", 30));
            _list1.Add(new MyItems("0001", "Advacado", 0));

            ObservableCollection<MyItems> _list2 = new ObservableCollection<MyItems>();

            _list2.Add(new MyItems("A001", "Deodorant", 10));
            _list2.Add(new MyItems("A002", "Toothpaste", 30));

            /* TODO: Develop logic that will add all items from _list1 that are not already present into _list2.  Items should be added into specific 
          * index positions according to the following rule: if DisplaySequence is not equal to zero, insert into the correct position according 
          * to that value; if DisplaySequence is equal to zero insert alphabetically according to the ItemName. The below foreach statement should
          * output in the following order:
          * 
          * 0001 - Advacado
          * A001 - Deodorant
          * A0ZZ - Hairspray
          * A002 - Toothpaste
          */

            foreach (var i in _list2)
            {
                Console.WriteLine(i.ItemName + " - " + i.ItemDescription);
            }

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }

    class MyItems
    {
        public MyItems(string _itemName, string _itemDescription, int _displaySequence)
        {
            ItemName = _itemName;
            ItemDescription = _itemDescription;
            DisplaySequence = _displaySequence;
        }

        public string ItemName { get; set; }
        public string ItemDescription { get; set; }
        public int DisplaySequence { get; set; }
    }
}
Posted
Updated 10-Dec-14 8:29am
v2
Comments
Tomas Takac 10-Dec-14 14:31pm    
We don't do your homework here. You need to show some effort first. For example by explaining what did you try and where are you stuck.
[no name] 10-Dec-14 14:41pm    
This isn't my homework. I was practicing it and struct while trying to get that output
Maciej Los 10-Dec-14 15:01pm    
OK, but you must remember that we are not freelancers. We can help you, but you have to make at least "something".

BillWoodruff 13-Dec-14 0:12am    
I'm ready to assist you with this, but i'd like to see you at least start working on implementing the 'Todo list tasks.

I suggest you start by figuring out how to merge the two lists and remove duplicates.

Have you used Linq ?

1 solution

Think of it. It's quite simple.
ObservableCollection has got several methods[^], like: Add, Insert, Contains, IndexOf. Use them to finish tasks listed on your ToDo list. ;)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Dec-14 16:10pm    
5ed.
—SA
Maciej Los 10-Dec-14 16:11pm    
Thank you, Sergey ;)
BillWoodruff 13-Dec-14 2:04am    
+2 Hi Maciej, There's nothing in the OP's question that uses any facility unique to ObservableCollection, like notification, and you do not address the two obvious things the OP wants to do: to eliminate duplicates, and to sort the result in a certain way.

Think about the Collection 'verbs you recommended: would a solution to either of these two tasks really need to use 'Contains or 'IndexOf ?

I admit I do "hold you to a higher standard here," in terms of expectation of quality in your solutions, as I wish you would hold me :)

respectfully, Bill
Maciej Los 13-Dec-14 3:56am    
Thank you for your opinion, Bill.
Honestly, if someone knows how to use IObservableCollection, he needs a bit hint instead a complete solution (or step-by-step guide). As you can see OP accepted my solution without any additional questions. That's why i'm thinking that your mark of 2 is a bit unfair.
I really do care about "higher standard". ;)

Cheers,
Maciej

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900