1

I have a simple custom object:

class CertQuestion
{
    public string Field {get;set;}
    public string Value {get;set;}
}

Subsequently I find myself with a List in some code. I'm trying to figure out how to format a list of CertQuestions into a corresponding Dictionary with similar Field names grouped together. For instance, given the following list:

        List<CertQuestion> certQuestions = new List<CertQuestion>()
            {
                new CertQuestion("Key", "Value1"),
                new CertQuestion("Key", "Value2"),
                new CertQuestion("Key2", "Value"),
                new CertQuestion("Key2", "Value2")
            };

I would like to convert that (trying to use LINQ) into a Dictionary with two entries such as

{{"Key", "Value1, Value2"}, {"Key2", "Value, Value2"}}
3
  • So you want a Dictionary<string,List<string>>? Commented Mar 10, 2015 at 17:28
  • 1
    So what problem(s) are you having creating this dictionary? Commented Mar 10, 2015 at 17:35
  • certQuestions.ToLookup(x => x.Field, x => x.Value); Commented Mar 10, 2015 at 17:41

2 Answers 2

13

Group the questions by field, then convert to dictionary by selecting key, then value. Value becomes the grouping's list.

certQuestions.GroupBy(c => c.Field)
             .ToDictionary(k => k.Key, v => v.Select(f => f.Value).ToList())

Or for an array:

certQuestions.GroupBy(c => c.Field)
             .ToDictionary(k => k.Key, v => v.Select(f => f.Value).ToArray())

Edit based on question in comment:

class CertTest 
{
    public string TestId {get;set;}
    public List<CertQuestion> Questions {get;set;}
}
var certTests = new List<CertTest>();

You would use the SelectMany extension method. It is designed to aggregate a property list object that is in each element of the original list:

certTests.SelectMany(t => t.Questions)
         .GroupBy(c => c.Field)
         .ToDictionary(k => k.Key, v => v.Select(f => f.Value).ToList())
Sign up to request clarification or add additional context in comments.

3 Comments

Not quite right, that'll give you a Dictionary<string,List<CertQuestion>>. You need to Select the Value. Something like .ToDictionary(k => k.Key, v => v.Select(cq => cq.Value).ToList())
Yes you are right, missed the string type vs. CertQuestion type in desired result.
It always seems so simple after someone has explained how to do it. Just out of curiosity, what if I had a list of objects and each object had a List<CertQuestion>, how could I wrap up all the objects' List<CertQuesion> fields into a single Dictionary<string, string[]>?
3

Your requirement was for a comma-separated list of values, that can be done like this:

var dict = certQuestions.GroupBy(c => c.Field)
             .ToDictionary(k => k.Key, v => String.Join(", ", v.Select(x => x.Value)))

Live example: http://rextester.com/LXS58744

(You should consider whether what you actually want is the values to be an Array or List<string> - see other answers)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.