从列表中的字典中获取值(Get Values from dictionary present in List)

我有一个清单,

List<string> list = new List<string>(); list.Add("MEASUREMENT"); list.Add("TEST");

我有一本字典,

Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("BPGA", "TEST"); dict.Add("PPPP", "TEST"); dict.Add("RM_1000", "MEASUREMENT"); dict.Add("RM_2000", "MEASUREMENT"); dict.Add("CDMA", "TEST"); dict.Add("X100", "XXX");

现在,我想根据列表从字典中获取所有匹配的数据。 意味着,列表中的所有数据都与dict值匹配,然后获得具有以下数学值的新字典

有没有办法通过使用lambda表达式来实现这一点?

我想要这样的结果。

Key Value "BPGA", "TEST" "PPPP", "TEST" "RM_1000", "MEASUREMENT" "RM_2000", "MEASUREMENT" "CDMA", "TEST"

提前致谢!

I have a list like,

List<string> list = new List<string>(); list.Add("MEASUREMENT"); list.Add("TEST");

I have a dictionary like,

Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("BPGA", "TEST"); dict.Add("PPPP", "TEST"); dict.Add("RM_1000", "MEASUREMENT"); dict.Add("RM_2000", "MEASUREMENT"); dict.Add("CDMA", "TEST"); dict.Add("X100", "XXX");

Now, I want to get all matched data from dictionary based on list. Means, all data from list match with dict value then get new dictionary with following mathched values

Is there any way to achieve this by using lambda expression?

I want result like this.

Key Value "BPGA", "TEST" "PPPP", "TEST" "RM_1000", "MEASUREMENT" "RM_2000", "MEASUREMENT" "CDMA", "TEST"

Thanks in advance!

最满意答案

您应该使用类似于要使用的字典,例如具有多个值的公共密钥,例如:

Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();

然后,添加值时您需要做的就是:

dict.Add("TEST", new List<string>() { /*strings go in here*/ });

然后从密钥获取所有结果,如:

List<string> testValues = dict["TEST"];

为了安全起见,你应该检查密钥是否存在,即

if (dict.ContainsKey("TEST")) { //Get the values }

然后,要为当前键添加值,您可以执行以下操作:

dict["TEST"].Add("NewValue");

如果你坚持保持相同的结构,虽然我不推荐它,如下所示的东西将起作用:

List<string> testKeys = new List<string>(); foreach (var pairs in dict) { if (pair.Value == "TEST") { testKeys.Add(pair.Key); } }

甚至以下LINQ语句:

List<string> testKeys = dict.Where(p => p.Value == "TEST").Select(p => p.Key).ToList();

对于查找列表中的查询的通用查询,请使用:

List<string> values = dict.Where(p => list.Contains(p.Value)).ToList();

You should be using the dictionary like it is intended to be used i.e. a common key with multiple values for example:

Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();

Then all you need to do when adding the values is:

dict.Add("TEST", new List<string>() { /*strings go in here*/ });

Then to get all the results from a key like:

List<string> testValues = dict["TEST"];

To make it safe however you should check that the key exists i.e.

if (dict.ContainsKey("TEST")) { //Get the values }

Then to add values to a current key you go do something like:

dict["TEST"].Add("NewValue");

If you insist on keeping the same structure, although I do not recommend it, something like the following will work:

List<string> testKeys = new List<string>(); foreach (var pairs in dict) { if (pair.Value == "TEST") { testKeys.Add(pair.Key); } }

Or even the following LINQ statement:

List<string> testKeys = dict.Where(p => p.Value == "TEST").Select(p => p.Key).ToList();

For a generic query to find the ones from your list use:

List<string> values = dict.Where(p => list.Contains(p.Value)).ToList();从列表中的字典中获取值(Get Values from dictionary present in List)

我有一个清单,

List<string> list = new List<string>(); list.Add("MEASUREMENT"); list.Add("TEST");

我有一本字典,

Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("BPGA", "TEST"); dict.Add("PPPP", "TEST"); dict.Add("RM_1000", "MEASUREMENT"); dict.Add("RM_2000", "MEASUREMENT"); dict.Add("CDMA", "TEST"); dict.Add("X100", "XXX");

现在,我想根据列表从字典中获取所有匹配的数据。 意味着,列表中的所有数据都与dict值匹配,然后获得具有以下数学值的新字典

有没有办法通过使用lambda表达式来实现这一点?

我想要这样的结果。

Key Value "BPGA", "TEST" "PPPP", "TEST" "RM_1000", "MEASUREMENT" "RM_2000", "MEASUREMENT" "CDMA", "TEST"

提前致谢!

I have a list like,

List<string> list = new List<string>(); list.Add("MEASUREMENT"); list.Add("TEST");

I have a dictionary like,

Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("BPGA", "TEST"); dict.Add("PPPP", "TEST"); dict.Add("RM_1000", "MEASUREMENT"); dict.Add("RM_2000", "MEASUREMENT"); dict.Add("CDMA", "TEST"); dict.Add("X100", "XXX");

Now, I want to get all matched data from dictionary based on list. Means, all data from list match with dict value then get new dictionary with following mathched values

Is there any way to achieve this by using lambda expression?

I want result like this.

Key Value "BPGA", "TEST" "PPPP", "TEST" "RM_1000", "MEASUREMENT" "RM_2000", "MEASUREMENT" "CDMA", "TEST"

Thanks in advance!

最满意答案

您应该使用类似于要使用的字典,例如具有多个值的公共密钥,例如:

Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();

然后,添加值时您需要做的就是:

dict.Add("TEST", new List<string>() { /*strings go in here*/ });

然后从密钥获取所有结果,如:

List<string> testValues = dict["TEST"];

为了安全起见,你应该检查密钥是否存在,即

if (dict.ContainsKey("TEST")) { //Get the values }

然后,要为当前键添加值,您可以执行以下操作:

dict["TEST"].Add("NewValue");

如果你坚持保持相同的结构,虽然我不推荐它,如下所示的东西将起作用:

List<string> testKeys = new List<string>(); foreach (var pairs in dict) { if (pair.Value == "TEST") { testKeys.Add(pair.Key); } }

甚至以下LINQ语句:

List<string> testKeys = dict.Where(p => p.Value == "TEST").Select(p => p.Key).ToList();

对于查找列表中的查询的通用查询,请使用:

List<string> values = dict.Where(p => list.Contains(p.Value)).ToList();

You should be using the dictionary like it is intended to be used i.e. a common key with multiple values for example:

Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();

Then all you need to do when adding the values is:

dict.Add("TEST", new List<string>() { /*strings go in here*/ });

Then to get all the results from a key like:

List<string> testValues = dict["TEST"];

To make it safe however you should check that the key exists i.e.

if (dict.ContainsKey("TEST")) { //Get the values }

Then to add values to a current key you go do something like:

dict["TEST"].Add("NewValue");

If you insist on keeping the same structure, although I do not recommend it, something like the following will work:

List<string> testKeys = new List<string>(); foreach (var pairs in dict) { if (pair.Value == "TEST") { testKeys.Add(pair.Key); } }

Or even the following LINQ statement:

List<string> testKeys = dict.Where(p => p.Value == "TEST").Select(p => p.Key).ToList();

For a generic query to find the ones from your list use:

List<string> values = dict.Where(p => list.Contains(p.Value)).ToList();