I am working on C# converting a dictionary to query :
public class myClass
{
public int my_id;
public Dictionary<string, Dictionary<string, string[]>> myDict;
}
Dictionary<string, myClass> dataDict;
var queryDict = (from happen in dataDict
group happen by happen.Value.my_id into g select g)
.ToDictionary(g => g.Key, g => g.ToDictionary(x => x.Key, x => x.Value));
I need to iterate every element of queryDict.
Before doing the query, for the same dataDict's key, there may be multiple myClass that have the same event_id.
After query, for the same happen.Value.my_id, how the dataDict' s original keys are organized ? They are a list pointed by the one event_id ?
Any help would be appreciated !
Basically group by
gives you an IGrouping<TKey, TElement>
which is an IEnumerable<TElement>
( IGrouping Interface ), so having this query:
from happen in dataDict group happen by happen.Value.my_id into g select g
means, you group by happen.Value.my_id
, so the TKey
will be my_id
and the TElement
would be a KeyValuePair<string, MyClass>>
which is the items of your dictionary. Since the IGrouping<int, KeyValuePait<stirng, MyClass>>
which is the result of above query is actually an IEnumerable<keyValuePair<string, MyClass>>
, so for every key you actually have an IEnumerable<KeyValuePair<string, MyClass>>
. As you can see here, you have the elements of you dictionary as they were. Second part of the query is just converting the IEnumerable<IGrouping<int, Dictionary<string, MyClass>>>
to a Dictionary<int, Dictionary<string, MyClass>>>
so it's not big deal.
Effectively, your dataDict is an array of KeyValuePair
(1-dimensonal array). And your query adds another dimension, so it becomes a Dictionary
of Dictionaries
. But adding another dimension is just a grouping of older elements by a new key.
It's better to illustrate it like this:
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.