LINQ:从Dictionary中获取给定值列表的键,反之亦然 [英] LINQ: Getting Keys for a given list of Values from Dictionary and vice versa

查看:250
本文介绍了LINQ:从Dictionary中获取给定值列表的键,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有以下结构 Dictionary< TKeys,TValues>数据;
我在两个数据类型上运行一些LINQ查询,并且经常需要在 Keys 之间切换。
获取给定值的键列表的最佳方式是什么?反之亦然
请注意,由于我以前的LINQ查询,我通常会有IEnumerable和IEnumerable,并且想要有一个类似于 IEnumerable&TK。 Dictionary.GetAllKeys(IEnumerable< IValues> vals) IEnumerable< TValues> Dictionary.GetAllValues(IEnumerable< IKeys> keys)

I have the following structure in my code Dictionary<TKeys, TValues> data;. I run some LINQ queries on both data types and often need to switch between Keys and Values. What is the best way to get the list of Keys for given Values and vice versa? Please, notice, that I usually have 'IEnumerable' and 'IEnumerable' as a result of my previous LINQ queries and would like to have something like IEnumerable<TKeys> Dictionary.GetAllKeys(IEnumerable<IValues> vals) and IEnumerable<TValues> Dictionary.GetAllValues(IEnumerable<IKeys> keys).

也许我需要这个任务的其他数据容器?

Maybe I need other data container for this task?

Regards,
Alexander。

Regards, Alexander.

推荐答案

 var values = dictionary.Where(x => someKeys.Contains(x.Key)).Select(x => x.Value);
 var keys = dictionary.Where(x => someValues.Contains(x.Value)).Select(x => x.Key);

这篇关于LINQ:从Dictionary中获取给定值列表的键,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆