Click here to Skip to main content
13,350,047 members (47,230 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
I have an object
Dictionary<int,> temp = new Dictionary<int,>();
The values in temp are:
1 s1
  s2
  s3
2 s1
  s2
I want to transform it into object
Dictionary<string,int[]> temp = new Dictionary< String,int[]>();
So i want final o/p as
s1 1
   2
s2 1
   2
s3 1



[Update]
(On behalf of the OP)
@CPillani.. ur code working cool thanks
If the input,o/p type changed as follows.. please update ur code ..
i/p = Dictionary<uri,> d1 = new Dictionary<uri,>();
o/p = Dictionary<string[],> socialTermUriNew = new Dictionary<string[],>();

[/Update]
Posted 7-May-12 1:38am
Updated 7-May-12 3:32am
CPallini418.1K
v2
Comments
HiDensity 7-May-12 6:43am
   
And what exactly is your problem?
Member 8937732 7-May-12 10:48am
   
Sample Input data

http:\\localhost\a1.aspx a1
a4
a5
http:\\localhost\a2.aspx a1
a5
http:\\localhost\a3.aspx a4

Would be transform to Final o/p

a1 http:\\localhost\a1.aspx
http:\\localhost\a2.aspx

a4 http:\\localhost\a1.aspx
http:\\localhost\a3.aspx

a5 http:\\localhost\a2.aspx


Input object

Dictionary<Uri, string[]> d1 = new Dictionary<Uri, string[]>();

Output Object

Dictionary<string, Uri[]> d2 = new Dictionary<string, Uri[]>();
Member 8937732 7-May-12 8:31am
   
@CPillani.. ur code working cool thanks
If the input,o/p type changed as follows.. please update ur code ..

i/p = Dictionary<Uri, String[]> d1 = new Dictionary<Uri, String[]>();
o/p = Dictionary<string[], Uri> socialTermUriNew = new Dictionary<string[], Uri>();
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

If you really need an array of integers the you have to iterate two times on the Dictionary.
e.g. (rd is the output Dictionary)

static void Main(string[] args)
    {
      string s1 = "s1";
      string s2 = "s2";
      string s3 = "s3";
      string[] a1 = { s1, s2, s3 };
      string[] a2 = { s1, s2 };
      

      Dictionary <int, string[]> d = new Dictionary<int, string[]>();
      d.Add(1, a1);
      d.Add(2, a2);
      

      Dictionary <string,int > cd = new Dictionary<string, int>();
      Dictionary <string, int> cdi = new Dictionary<string, int>();
      
      foreach (int k in d.Keys)
      {
        foreach (string s in d[k]) 
        {
          if (cd.ContainsKey(s))
            cd[s]++;
          else
            cd.Add(s, 1);
        }  
      }
      Dictionary<string, int[]> rd = new Dictionary<string, int[]>();


      foreach (string s in cd.Keys)
      {
        rd.Add(s, new int[cd[s]]);
        cdi.Add(s, 0);
      }

      

      foreach (int k in d.Keys)
      {
        foreach (string s in d[k])
        {
          rd[s][cdi[s]++] = k;
        }
      }
    }


[Update]
I don't get your new requirements completely (I cannot see proper declaration of your generic types), but I suppose you may modify the code I provided to fit your needs (it shouldn't be hard).
[/Update]
  Permalink  
v3
Comments
Member 8937732 7-May-12 8:48am
   
@CPillani.. ur code working cool thanks

If the input,o/p type changed as follows.. please update ur code ..

i/p = Dictionary<uri,string[]> d1 = new Dictionary<uri,>();

o/p = Dictionary<string[],Uri> socialTermUriNew = new Dictionary<string[],>();
CPallini 7-May-12 8:55am
   
Please stop posting additional requirements as fake answers. Modify the original post instead (and try harder to post the actual code, without messign with HTML tags).
Member 8937732 7-May-12 12:46pm
   
Thanks a lot its done... u r champ ...
CPallini 7-May-12 12:53pm
   
You are welcome.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

I think you should use property classes

public class UserDefinedDictionary{
public string strText =new string();
public int[5] intArr=new int[5];
}

Dictionary<userdefineddictionary> udDictionary=new Dictionary<userdefineddictionary>();

udDictionary.strText="s1";
udDictionary.intArr[0]=1;
udDictionary.intArr[1]=2;

udDictionary.strText="s2"
udDictionary.intArr[0]=1;
udDictionary.intArr[1]=2;

udDictionary.strText="s3";
udDictionary.intArr[0]=1;

hope this might help you.

Do let me know if there is some syntax issue.

Best Regards;
Nazish Abbasi
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.180111.1 | Last Updated 7 May 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100