简体   繁体   中英

How can I add additional Dictionary object

How Can I Add additional Dictionary Object below

var topLevel1 = resultRows.GroupBy(g => g["CustomerAccountType"])
.ToDictionary(g => g.Key, g => g.ToList());

I want to add this within the LINQ line

  new Dictionary<string, object> { {"children", someDictionaryAgain}}

I want to append additional dictionary object after the .ToDictionary()

something like

var topLevel1 = resultRows.GroupBy(g => g["CustomerAccountType"])
.ToDictionary(g => g.Key, g => g.ToList()).Add(new Dictionary<string, object> {     {"somekey", "somevalueorobject"}}

Here is the expected output I wanted.

var toplevel = Dictionary <stirng, object> {
  {"Actual1", 0},
  {"Actual2", 0}
 }

After .ToDictionary() --> What code is best to use to achieve

    var toplevel = Dictionary <stirng, object> {
      {"Actual1", 0},
     {"Actual2", 0},
     {"AddedDynamically",0}
 }

ToDictionary outputs a Dictionary , so you can easily save the result into a variable and add to it. To ensure you're getting a string and object dictionary, though, you'll need to explicitly reference the types.

var topLevel1 = resultRows
    .GroupBy(g => g["CustomerAccountType"])
    .ToDictionary(
        g => g.Key,                                // This is fine since it returns a string
        g => { return g.ToList() as object; });    // Explicitlyreturn this as an object
topLevel1.Add("somekey", "somevalueorobject");

To expand nested collections, use SelectMany instead of Select

var topLevel1 = resultRows
    .GroupBy(g => g["CustomerAccountType"])
    .SelectMany(g => g.Select(v => new { Key = g.Key, Value = v })) // This creates an anonymous type for use further into the query
    .ToDictionary(
        g => g.Key,
        g => g.Value);

You can't do this, for multiple reasons.

Primarily, because ToDictionary returns a new dictionary object (which you then assign). Calling Add on this returns void , so you can't do the assignment.

Basically, you have to leave your first line alone. To do the merge, you need to do a foreach. This part of the question has been discussed before at: Merging dictionaries in C#

Basically you end up with a loop:

foreach (KeyValuePair<string, object> kvp in secondDictionary)
    topLevel1.Add(kvp.Key, kvp.Value);

Note that the code above will break on a duplicate key.

you can try something like this:

var topLevel1 = resultRows
.GroupBy(g => g["CustomerAccountType"])
.ToDictionary(
    g => g.Key,                                
    g => { return g.ToList() as object; }).Union(new Dictionary<string, object> {     {"somekey", "somevalueorobject"}).ToDictionary(x=>x.Key,x=>x.Value);

If you use something like:

Dictionary<string,Dictionary<string,object>>

then you have the dictionary you are looking for ...

public class Test
{
    public static void test()
    {
        Dictionary<string, int> d1 = new Dictionary<string, int>();
        Dictionary<string, int> d2 = new Dictionary<string, int>();

        d1.AddRange(d2);
    }
}

public static class Extensions
{
    public static Dictionary<K, V> AddRange<K, V>(this Dictionary<K, V> d1, Dictionary<K, V> d2)
    {
        foreach (var kv in d2)
        {
            d1[kv.Key] = kv.Value;
        }
        return d1;
    }
}

Using this extension function will:

  • not fail upon duplicate key
  • return the starting dictionary so you have a fluent interface

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM