如何在 C# 中的 List<Tuple<string,string>> 中搜索字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14134139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Search a String in List<Tuple<string,string>> in C#
提问by Aravind
I am having a
我有一个
List<Tuple<string,string>> tr = new List<Tuple<string,string>>();
tr.Add(new Tuple<string, string>("Test","Add");
tr.Add(new Tuple<string, string>("Welcome","Update");
foreach (var lst in tr)
{
if(lst.Contains("Test"))
MessageBox.Show("Value Avail");
}
I failed while doing this ,....
我这样做失败了,....
采纳答案by horgh
Probably this should work:
可能这应该有效:
foreach (var lst in tr)
{
if (lst.Item1.Equals("Test"))
MessageBox.Show("Value Avail");
}
or this
或这个
if (lst.Item1.Equals("Test") || lst.Item2.Equals("Test"))
Read Tuple Class; you'll need to access values of the tuple through Item1
and/or Item2
properties.
读取元组类;您需要通过Item1
和/或Item2
属性访问元组的值。
And why use Tuple at all? Maybe this is easier:
为什么要使用元组?也许这更容易:
Dictionary<string, string> dict = new Dictionary<string, string>
{
{"Test", "Add"},
{"Welcome", "Update"}
};
if (dict.ContainsKey("Test"))
{
MessageBox.Show("Value Avail:\t"+dict["Test"]);
}
回答by Ameen
it should be foreach (var lst in tr)
not lstEvntType and you should test for the tuple's Item1 field instead.
它不应该是foreach (var lst in tr)
lstEvntType 并且您应该测试元组的 Item1 字段。
回答by boindiil
Why are you iterating over lstEvntType and not tr? You should try this:
为什么要迭代 lstEvntType 而不是 tr?你应该试试这个:
List<Tuple<string,string>> tr = new List<Tuple<string,string>>();
tr.Add(new Tuple<string, string>("Test","Add"));
tr.Add(new Tuple<string, string>("Welcome","Update"));
List<Tuple<string,string>> lstEvntType = new List<Tuple<string,string>>();
foreach (var lst in tr)
{
if(lst.Item1.Equals("Test"))
MessageBox.Show("Value Avail");
}
回答by Tilak
Change
改变
if(lst.Contains("Test"))
To
到
if(lst.Item1.Contains("Test") || lst.Item2.Contains("Test"))
If tuple has more items, you need to add condition for all items.
如果元组有更多项,则需要为所有项添加条件。
If you want to make it common for all tuples, you need to use Reflection (and the quirky way).
如果你想让它对所有元组通用,你需要使用反射(和古怪的方式)。
回答by jam40jeff
If you'd like to use LINQ:
如果您想使用 LINQ:
if(tr.Any(t => t.Item1 == "Test" || t.Item2 == "Test"))
MessageBox.Show("Value Avail");
This will also have the benefit of only showing the message box once if the text is found multiple times (if that is what is desired).
如果文本被多次找到(如果这是需要的),这也将具有仅显示一次消息框的好处。
回答by Sunsetquest
Maybe this might help someone else. Here is the method I went with:
也许这可能会帮助别人。这是我采用的方法:
List<Tuple<string,string>> tr = new List<Tuple<string,string>>();
tr.Add(new Tuple<string, string>("Test","Add");
tr.Add(new Tuple<string, string>("Welcome","Update");
if(lst.Any(c => c.Item1.Contains("Test")))
MessageBox.Show("Value Avail");
(credit goes here)
(信用在这里)
回答by Hymanypengyu
List<Tuple<string,string>> tr = new List<Tuple<string,string>>();
tr.Add(new Tuple<string, string>("Test","Add");
tr.Add(new Tuple<string, string>("Welcome","Update");
var index = tr.FindIndex(s=>s.Item1 == "Test" || s.Item2 == "Test");
if(index != -1)
MessageBox.Show("Value Avail");
Using FindIndex, you can check the availability and index of element at the same time.
使用 FindIndex,您可以同时检查元素的可用性和索引。