Solved

Compare 2 collections

Posted on 2009-05-12
4
634 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

695 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