Solved

Compare 2 collections

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

860 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