[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Compare 2 collections

Posted on 2009-05-12
4
Medium Priority
?
636 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 2000 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 2000 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 2000 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

Industry Leaders: 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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

656 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