Click here to Skip to main content
15,914,452 members
Home / Discussions / C#
   

C#

 
AnswerRe: computer graphics Pin
Henry Minute31-Jul-09 9:41
Henry Minute31-Jul-09 9:41 
AnswerRe: computer graphics Pin
Luc Pattyn31-Jul-09 18:43
sitebuilderLuc Pattyn31-Jul-09 18:43 
QuestionSocket Authentication in C# Pin
joana.simoes31-Jul-09 5:35
joana.simoes31-Jul-09 5:35 
Questionhow to convert list to dictionary Pin
hotthoughtguy31-Jul-09 3:55
hotthoughtguy31-Jul-09 3:55 
AnswerRe: how to convert list to dictionary Pin
Henry Minute31-Jul-09 4:08
Henry Minute31-Jul-09 4:08 
GeneralRe: how to convert list to dictionary Pin
hotthoughtguy31-Jul-09 4:12
hotthoughtguy31-Jul-09 4:12 
GeneralRe: how to convert list to dictionary Pin
hotthoughtguy31-Jul-09 4:14
hotthoughtguy31-Jul-09 4:14 
GeneralRe: how to convert list to dictionary [modified] Pin
Henry Minute31-Jul-09 5:06
Henry Minute31-Jul-09 5:06 
try this:

public partial class ListToDictionaryTestForm : Form
{
    List<string> testList = new List<string>();

    public ListToDictionaryTestForm()
    {
        InitializeComponent();

        testList.Add("one");
        testList.Add("One");
        testList.Add("two");
        testList.Add("three");
        testList.Add("one");
        testList.Add("three");
        testList.Add("one");

        OutputResults(testList);
    }

    private void OutputResults(List<string> testList)
    {
        var itemcounts = from s in testList
                         group s by s into g
                         select new
                         {
                             key = g.Key,
                             val = g.Count()
                         };
        Dictionary<string, int> results = itemcounts.ToDictionary(k => k.key, v => v.val);

        foreach (KeyValuePair<string, int> kvp in results)
        {
            lboxResults.Items.Add(string.Format("{0} : {1}", kvp.Key, kvp.Value));
        }
    }
}


This is just a Form with a ListBox on it.

I've just realized that this can be abbreviated slightly:

private void OutputResults(List<string> testList)
{
    Dictionary<string, int> results = (from s in testList
                    group s by s into g
                    select new
                    {
                        key = g.Key,
                        val = g.Count()
                    }).ToDictionary(k => k.key, v => v.val);

    foreach (KeyValuePair<string, int> kvp in results)
    {
        lboxResults.Items.Add(string.Format("{0} : {1}", kvp.Key, kvp.Value));
    }
}


Henry Minute

Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”

modified on Friday, July 31, 2009 11:38 AM

GeneralRe: how to convert list to dictionary Pin
Luc Pattyn31-Jul-09 9:18
sitebuilderLuc Pattyn31-Jul-09 9:18 
GeneralRe: how to convert list to dictionary Pin
Henry Minute31-Jul-09 9:22
Henry Minute31-Jul-09 9:22 
GeneralRe: how to convert list to dictionary Pin
Luc Pattyn31-Jul-09 10:01
sitebuilderLuc Pattyn31-Jul-09 10:01 
GeneralRe: how to convert list to dictionary Pin
Henry Minute31-Jul-09 10:11
Henry Minute31-Jul-09 10:11 
GeneralRe: how to convert list to dictionary Pin
Luc Pattyn31-Jul-09 10:29
sitebuilderLuc Pattyn31-Jul-09 10:29 
GeneralRe: how to convert list to dictionary Pin
Dan Neely31-Jul-09 9:26
Dan Neely31-Jul-09 9:26 
AnswerRe: how to convert list to dictionary Pin
Super Lloyd31-Jul-09 4:18
Super Lloyd31-Jul-09 4:18 
GeneralRe: how to convert list to dictionary Pin
hotthoughtguy31-Jul-09 4:28
hotthoughtguy31-Jul-09 4:28 
AnswerRe: how to convert list to dictionary Pin
0x3c031-Jul-09 4:38
0x3c031-Jul-09 4:38 
GeneralRe: how to convert list to dictionary Pin
hotthoughtguy31-Jul-09 4:44
hotthoughtguy31-Jul-09 4:44 
AnswerRe: how to convert list to dictionary Pin
Dave Kreskowiak31-Jul-09 5:05
mveDave Kreskowiak31-Jul-09 5:05 
QuestionProgrammatically updating application properties or help creating my own xml file please. Pin
JollyMansArt31-Jul-09 2:51
JollyMansArt31-Jul-09 2:51 
AnswerRe: Programmatically updating application properties or help creating my own xml file please. Pin
Super Lloyd31-Jul-09 2:55
Super Lloyd31-Jul-09 2:55 
GeneralRe: Programmatically updating application properties or help creating my own xml file please. Pin
Henry Minute31-Jul-09 3:57
Henry Minute31-Jul-09 3:57 
GeneralRe: Programmatically updating application properties or help creating my own xml file please. Pin
Super Lloyd31-Jul-09 4:03
Super Lloyd31-Jul-09 4:03 
JokeRe: Programmatically updating application properties or help creating my own xml file please. Pin
JollyMansArt1-Aug-09 2:33
JollyMansArt1-Aug-09 2:33 
GeneralRe: Programmatically updating application properties or help creating my own xml file please. Pin
Super Lloyd1-Aug-09 16:53
Super Lloyd1-Aug-09 16:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.