Solved

Compare 2 collections

Posted on 2009-05-12
4
627 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

17 Experts available now in Live!

Get 1:1 Help Now