Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 7-May-12 2:32am
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="">();

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]
 
Share this answer
 
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.
I think you should use property classes

C#
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900