I would like a method to return a Dictionary of Dictionary from an IQueryable formula. Typically,
Dictionary<int, Dictionary<DateTime, string>>
At first, I looked at ToDictionary() of course but couldn't find a way to declare two one after the other.
Then, i loooked at ToLookup() and I have a way to carry this with a ILookup with the string being my secondary dictionary(the DateTime.Tostring() + the other string)... you follow ? :) But I don't find the ILookup solution really confortable (parsing a date to string and when i receive the data -> string to date.. beuark)
That would give something like that
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 about creating a List of self created class with the 3 informations I need as properties.
Could you help me chosing the best pattern ? This method is hosted on a windows service and I would like to limit the amount of data transfered to the client.
Thank you
Here's an example, using GroupBy
and 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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With