【发布时间】:2016-09-02 04:28:10
【问题描述】:
我有一个 C# 列表:
private List<Tuple<string, string>> _list1 = new List<Tuple<string,string>>();
string keyToLookFor;
string valueToLookFor;
我想使用 LINQ 搜索这个列表,首先检查某个键是否已经存在,如果存在,它的值是否与 valueToLookFor 匹配。
List.Add(new Tuple<string, string>("1", "data1"));
List.Add(new Tuple<string, string>("2", "data2"));
List.Add(new Tuple<string, string>("2", "data3"));
List.Add(new Tuple<string, string>("3", "data3"));
所以对于这个例子,我想找到“2”是否存在,并检查它的数据是否与我正在寻找的匹配。 如何在 LINQ 中执行此操作?
【问题讨论】:
-
为什么不创建一个类我认为在你的情况下它会容易得多..你也做过任何关于如何使用 Linq 从元组中读取值的谷歌搜索......如果你想这样做有一个类参考这个以前的帖子 - stackoverflow.com/questions/10597629/…
-
Dictionary
听起来像你要找的东西 -
字典可以有非唯一键吗??
-
@HarryBoy 不,它不能,但你总是可以有一个
Dictionary<string, List<string>>