-3

I have a dictionary with string and int and I would like to parse the dictionary string and ints to a html table if that is possible?

protected void Page_Load(object sender, EventArgs e)
    {

        Dictionary<string, int> userMediaList = new Dictionary<string, int>();
        userMediaList.Add("Die Hard", 5);
        userMediaList.Add("Die Hard II", 3);
        userMediaList.Add("Die Hard III", 2);
        userMediaList.Add("Ringenes Herre", 4);
        userMediaList.Add("Ringenes Herre II", 1);
        userMediaList.Add("Ringenes Herre III", 5);

        var sortUserMediaList = from entry in userMediaList orderby entry.Value ascending select entry;

    }

So what i want is adding this to a table on the website.

So that for every box in the table, the name of the movie and the rating of the movie will appear.

dav_i
  • 25,285
  • 17
  • 94
  • 128
Winkz
  • 331
  • 2
  • 5
  • 18
  • 4
    Of course it's possible. It depends on the technologies you're using... – Andrei V Dec 03 '13 at 15:08
  • 1
    Geez Andrei V. whats up with the hate.... @Winkz, you might want to take a look at http://stackoverflow.com/questions/2274875/bind-dictionary-to-repeater , they bind a dictionnary to a repeater. that could easily do exactly what you want. – Dave Dec 03 '13 at 15:09
  • Is it in a web application? – Ufuk Hacıoğulları Dec 03 '13 at 15:09
  • Okay thats good i guess :D well atm im looping through the entries with a foreach loop.. just dont know how to apply each line to a new "box" in the table. – Winkz Dec 03 '13 at 15:09
  • Thank you Dave, gonna try and read that! Ufuk it's a webpage, so yea i suppose :) – Winkz Dec 03 '13 at 15:10
  • @Winkz, not really intentional. But was I not answering the question? Although grammar "enthusiasts" would agree that (at least in the first sentence) the "question" is not really a question... – Andrei V Dec 03 '13 at 15:12
  • Ill edit it then i guess if you are picky about it ;p – Winkz Dec 03 '13 at 15:14
  • @Dave I had a look at it and cant really figure out, how that would help me.. Totally new at parsing things from code behind :/ – Winkz Dec 03 '13 at 15:15

2 Answers2

6

As you don't state what technology you are using, this is a generic way:

var trs = dictionary.Select(a => String.Format("<tr><td>{0}</td><td>{1}</td></tr>", a.Key, a.Value));

var tableContents = String.Concat(trs);

var table = "<table>" + tableContents + "</table>";
dav_i
  • 25,285
  • 17
  • 94
  • 128
2

You can start with that, but there are probably better methods:

string html = "<table>";
    foreach (KeyValuePair<string,int> pair in userMediaList)
    {
        html = html + String.Format("<tr><td>{0}</td><td>{1}</td></tr>", pair.Key, pair.Value.ToString());
    }
html = html + "</table>";
AdamL
  • 10,453
  • 5
  • 46
  • 68
  • Nothing comes up from adding this to the code :/ Do i need to add something in the html part? – Winkz Dec 03 '13 at 15:29