managing counts

  • Thread starter Thread starter ArunDhaJ
  • Start date Start date
A

ArunDhaJ

Hi
I need to manage count as follows:

MainItem1 SubItem1 - 10
MainItem1 SubItem2 - 5
MainItem1 SubItem3 - 10
MainItem2 SubItem1 - 15
MainItem3 SubItem1 - 10
MainItem3 SubItem2 - 9

what would be the best datastructure to store such data.?

Thanks in advance
Regards
ArunDhaJ
 
ArunDhaJ,

You could always create a structure with two fields, MainItem and
SubItem, then use that as a key in a dictionary, where the value is an int.

Then, you can just add to the value in the dictionary as you come across
your fields.

Of course, if you are using LINQ, you could easily query your collection
and group on MainItem and SubItem and select the count for each group.
 
Thanks Nicholas, I'm using C# 2.0.
After storing the key and value how can we query the dictionary to get
the total counts for individual Items. From the example in previous
posts, I need to get the total count of MainItem1 as 25, MainItem2 as
15 and MainItem3 as 19.
Thanks in advance

-ArunDhaJ


ArunDhaJ,

    You could always create a structure with two fields, MainItem and
SubItem, then use that as a key in a dictionary, where the value is an int..

    Then, you can just add to the value in the dictionary as you come across
your fields.

    Of course, if you are using LINQ, you could easily query your collection
and group on MainItem and SubItem and select the count for each group.

--
          - Nicholas Paldino [.NET/C# MVP]
          - (e-mail address removed)




Hi
I need to manage count as follows:
MainItem1 SubItem1 - 10
MainItem1 SubItem2 - 5
MainItem1 SubItem3 - 10
MainItem2 SubItem1 - 15
MainItem3 SubItem1 - 10
MainItem3 SubItem2 - 9
what would be the best datastructure to store such data.?
Thanks in advance
Regards
ArunDhaJ- Hide quoted text -

- Show quoted text -
 
Well, you are going to have to cycle through all of the items, and
increment the value in the dictionary one at a time as you come across the
MainItem/SubItem combinations. As you cycle through every item (when
generating the count), if the key exists, then get the value, increment by
one, and then set the value back.

If the key doesn't exist, insert one for that key into the dictionary.

- Nick

Thanks Nicholas, I'm using C# 2.0.
After storing the key and value how can we query the dictionary to get
the total counts for individual Items. From the example in previous
posts, I need to get the total count of MainItem1 as 25, MainItem2 as
15 and MainItem3 as 19.
Thanks in advance

-ArunDhaJ


ArunDhaJ,

You could always create a structure with two fields, MainItem and
SubItem, then use that as a key in a dictionary, where the value is an
int.

Then, you can just add to the value in the dictionary as you come across
your fields.

Of course, if you are using LINQ, you could easily query your collection
and group on MainItem and SubItem and select the count for each group.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




Hi
I need to manage count as follows:
MainItem1 SubItem1 - 10
MainItem1 SubItem2 - 5
MainItem1 SubItem3 - 10
MainItem2 SubItem1 - 15
MainItem3 SubItem1 - 10
MainItem3 SubItem2 - 9
what would be the best datastructure to store such data.?
Thanks in advance
Regards
ArunDhaJ- Hide quoted text -

- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top