Solved

Compare 2 collections

Posted on 2009-05-12
4
629 Views
Last Modified: 2012-05-06
I have 2 collections I want to compare. The collections contains the same objects, but it is sorted different based on a input date:

//Get the collection based on date:
ItemCollection ItemCollection_1 = ItemHandler.GetItems(05/05/2009);
ItemCollection ItemCollection_2 = ItemHandler.GetItems(05/12/2009);


//Here is how I bind the collection to a DataGrid:
this.dtgList.DataSource = ItemCollection_2;
this.dtgList.DataBind();


Example ItemCollection_1 (5th of May):

Id Title  Score
1  hi      30
2  hello  20
3  test   10


Example ItemCollection_2 (12th of May):

Id Title  Score
1  test   50
2  hi       20
3  hello  10


What I need to do is to show "yes" or "no" in the datagrid (make a column called "Increase") if the score is highter or lower than the score last week (ItemCollection_1).
0
Comment
Question by:webressurs
  • 3
4 Comments
 
LVL 3

Assisted Solution

by:demi-os
demi-os earned 500 total points
ID: 24366242
Please notice that i didnt tested it but it should work.

Assuming you added a column for your increased results to dtgList and your ItemCollection has items like:
 

public class ItemData

{

    public string Id { get; set; }

    public string Title { get; set; }

    public string Score { get; set; }

    public string Increase { get; set; }

}

// this will find, compare and update your ItemCollection_2

foreach (ItemData _item in ItemCollection_2)

{

    ItemData _olditem = ItemCollection_1.Single(s => s.Category.Equals(_item.Title));

    if ((_item.Title == _olditem.Title) && (_item.Score > _olditem.Score)) 

        _item.Increase = "yes"; 

    else

        _item.Increase = "no"; 

}

Open in new window

0
 
LVL 3

Accepted Solution

by:
demi-os earned 500 total points
ID: 24366573
It looks like i forgot to check if it has the same title at
line 17:  _item.Increase = "no";
should be replaced with:
if (_item.Title == _olditem.Title)  _item.Increase = "no";
foreach (ItemData _item in ItemCollection_2)

{

    ItemData _olditem = ItemCollection_1.Single(s => s.Category.Equals(_item.Title));

    if ((_item.Title == _olditem.Title) && (_item.Score > _olditem.Score)) 

        _item.Increase = "yes"; 

    else

        if (_item.Title == _olditem.Title) _item.Increase = "no";

}

Open in new window

0
 
LVL 1

Author Comment

by:webressurs
ID: 24374180
Hi! I tried to use your code, but dont understand the ".Single" property (ItemCollection_1.Single)? It dont exist...?

I fixed it by using a Dictionary like this:

ItemCollection ItemCollection = ItemHandler.GetItems(05/07/2009);
ItemCollection ItemCollectionCompare = ItemHandler.GetItems(05/01/2009);

Dictionary<int, int> oldItem = new Dictionary<int, int>();

foreach (Item item in ItemCollectionCompare)
{
    oldItem.Add(item.Id, item.Score);
}

ItemCollection newItems = new ItemCollection();
foreach (Item item in ItemCollection)
{
    Item newItem = new Item();

    newItem.Id = item.Id;
    newItem.Title = item.Title;
    newItem.Text = item.Text;
    newItem.Score = item.Score;


    //New list

        if (item.Score < oldItem[item.Id])
        {
            newItem.Result = "Down";
        }
        else if (oldItem[item.Id] == item.Score)
        {
            newItem.Result = "Same";
        }
        else
        {
            newItem.Result = "Up";
        }


    newItems.add(newItem);      

}

this.dtgList.DataSource = newItems;
0
 
LVL 3

Assisted Solution

by:demi-os
demi-os earned 500 total points
ID: 24375282
I am glad you found a solution.
Single is a method from System.Linq namespace:
http://msdn.microsoft.com/en-us/library/system.linq.enumerable.single.aspx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now