简体   繁体   中英

c# `dictionary of dictionary` query

could someone tell me the correct way to query this:

dictionary of dictionary

Dictionary<int, Dictionary<Guid, AutoStopWatchAndLog>> _dicDictionaryThread

where what i am looking for is from any of the first level and then from any item in the second where the level is less than x

dics betlow is: Dictionary<int, Dictionary<Guid, AutoStopWatchAndLog>>

var mostlikey = dics.FirstOrDefault(x=>x.Value.Where(y=>y.Value.Level > x));

If you want to project to a new dictionary of dictionaries filtered to the desired items, you will need to project both levels of dictionaries, which would look something like:

var query = _dicDictionaryThread.Select(o => new {o.Key, Value = o.Value
                                                                  .Where(y=>y.Value.Level > x)
                                                                  .ToDictionary(y => y.Key, y => y.Value)})
                                .Where(o => o.Value.Any())
                                .ToDictionary(o => o.Key, o => o.Value);

If you can easily understand this and explain it to someone else, go for it, otherwise just use a traditional loop - you're not going to get any performance boost from Linq and it will likely take longer to decipher.

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