Dictionary Dictionary from IQueryable

I would like the method to return a dictionary dictionary from an IQueryable formula. Usually,

Dictionary<int, Dictionary<DateTime, string>>

At first I looked at ToDictionary (), but could not find a way to declare two after another.

Then I looked at ToLookup (), and I have a way to wrap this with ILookup, where the line is my secondary dictionary (DateTime.Tostring () + another line) ... you follow? :) But I didn’t find the ILookup solution really confortable (parsing a date on a string and when I get a data string → for today .. beuark)

It will give something like this

return this.ttdc.Trackers.GroupBy(t => new { TrackerTaskId = t.TrackerTaskID, DateGenerated = t.TrackerDateGenerated })
                    .Select(t => new { taskId = t.Key.TrackerTaskId, DateGenerated = t.Key.DateGenerated, count = t.Count() })
                    .ToLookup(k => k.taskId.Value, k => k.DateGenerated.Value.ToString() + "|" + k.count);

Now I'm thinking of creating a list of a self-created class with 3 information that I need as properties.

Could you help me choose the best sample? This method is hosted on a Windows service, and I would like to limit the amount of data transferred to the client.

+5
1

, GroupBy ToDictionary:

var testValues = new[]{new {ID = 1, Date = new DateTime(2010,1,1), Str = "Val1"},
                       new {ID = 1, Date = new DateTime(2011,2,2), Str = "Val2"},
                       new {ID = 2, Date = new DateTime(2010,1,1), Str = "Val3"}};
var dict = testValues.GroupBy(item => item.ID)
                     .ToDictionary(grp => grp.Key, 
                                   grp => grp.ToDictionary(item => item.Date, 
                                                           item => item.Str));
+4

All Articles