Usman Khan
Usman Khan

Reputation: 686

Initialising a new Dictionary with a List of keys

I am initialising a new Dictionary and putting each key one by one like so:

igData = data.Select(x => new Dictionary<string, string> {
                     ["Date"] = x.GetValueOrDefault("DATE"),
                     ["SP"] = x.GetValueOrDefault("SP"),
                     ["IG"] = x.GetValueOrDefault("IG")}).ToList();

This is fine but the rest of the keys need to be headers that I have created in List<string> headers

Is it possible to add this list of headers within the initialisation above? Something like this:

igData = data.Select(x => new Dictionary<string, string> {
     ["Date"] = x.GetValueOrDefault("DATE"),
     ["SP"] = x.GetValueOrDefault("SP"),
     ["IG"] = x.GetValueOrDefault("IG"),
     headers.Select(y => new KeyValuePair<string, string>
                                             (y, x.GetValueOrDefault["IG"])).ToList();

I understand the value is the same but that is intended. Is the above possible or is this something I would have separate. It would be ideal to do it in the above since I have access to the value from the data collection.

Upvotes: 1

Views: 923

Answers (4)

Enigmativity
Enigmativity

Reputation: 117029

I like this way:

igData =
    data
        .Select(x =>
            Enumerable
                .Concat(
                    headers
                        .GetHalfHourlyTimeHeaders()
                        .Select(k => new { k, v = x.GetValueOrDefault("IG") }),
                    new [] { "Date", "SP", "IG" }
                        .Select(k => new { k, v = x.GetValueOrDefault(k.ToUpper()) }))
                .ToDictionary(z => z.k, z => z.v))
        .ToList();

Upvotes: 0

Jamiec
Jamiec

Reputation: 136074

You cant do it in an initializer like that, but I'd suggest you can do it with a little bit of Concat and ToDictionary:

igData = data.Select(x => {
   return new KeyValuePair<string,string>[]{
          new KeyValuePair<string,string>("Date", x.GetValueOrDefault("DATE")),
          new KeyValuePair<string,string>("SP", x.GetValueOrDefault("SP")),
          new KeyValuePair<string,string>("IG", x.GetValueOrDefault("IG"))
       }.Concat(
          headers.GetHalfHourlyTimeHeaders().Select(y => new KeyValuePair<string, string>
                                             (y, x.GetValueOrDefault["IG"])).ToList()
       ).ToDictionary(k => k.Key, v => v.Value);
});

Upvotes: 3

Joelius
Joelius

Reputation: 4329

Similar to the answer of Jamiec but in my opinion more readable (but not tested for performance):

List<Dictionary<string, string>> igData = data.Select(x => 
    {
        var dict = new Dictionary<string, string>
        {
            ["Date"] = x.GetValueOrDefault("DATE"),
            ["SP"] = x.GetValueOrDefault("SP"),
            ["IG"] = x.GetValueOrDefault("IG")
        };

        // I hope I understand correctly that GetHalfHourlyTimeHeaders returns List<string>
        foreach (string header in headers.GetHalfHourlyTimeHeaders())
        {
            dict.Add(header, x.GetValueOrDefault("IG"));
        }

        return dict;
    }
).ToList();

Upvotes: 0

Cheng Chen
Cheng Chen

Reputation: 43513

igData = data.Select(x =>
    new Dictionary<string, string>(headers.GetHalfHourlyTimeHeaders().ToDictionary(y => y, y => x.GetValueOrDefault("IG")))
    {
        ["Date"] = x.GetValueOrDefault("DATE"),
        ["SP"] = x.GetValueOrDefault("SP"),
        ["IG"] = x.GetValueOrDefault("IG")
    });

Query syntax looks better to me

igData = from x in data
         let ig = x.GetValueOrDefault("IG")
         select new Dictionary<string, string>(headers.GetHalfHourlyTimeHeaders().ToDictionary(y => y, y => ig))
         {
             ["Date"] = x.GetValueOrDefault("DATE"),
             ["SP"] = x.GetValueOrDefault("SP"),
             ["IG"] = ig
         };

Upvotes: 3

Related Questions