C# 如何在C中搜索列表#

C# 如何在C中搜索列表#,c#,arrays,search,C#,Arrays,Search,我有这样一份清单: List<string[]> countryList 如何搜索特定数组的countryList,例如如何搜索countryList new string[3] { "GB", "United Kingdom", "United Kingdom" }? //返回匹配数组的索引,如果找不到合适的数组,则返回-1 公共静态intFindIndex(列出所有字符串,字符串[]字符串) { 返回allString.FindIndex(pt=>IsStringEqual(

我有这样一份清单:

List<string[]> countryList
如何搜索特定数组的
countryList
,例如如何搜索
countryList

new string[3] { "GB", "United Kingdom", "United Kingdom" }?
//返回匹配数组的索引,如果找不到合适的数组,则返回-1
公共静态intFindIndex(列出所有字符串,字符串[]字符串)
{
返回allString.FindIndex(pt=>IsStringEqual(pt,string));
}
私有静态bool IsStringEqual(字符串[]str1,字符串[]str2)
{
if(str1.Length!=str2.Length)
返回false;
//在这里逐个元素进行比较
for(int i=0;i
要简单地建立存在,请使用
countryList.Any

要查找元素的索引或-1(如果不存在),请使用
countryList.FindIndex

错误。。。Equals方法比较引用,而不是数组中的值,因此它将永远找不到任何内容。如果我缺少什么内容,因为我的列表对象上没有FirstOrDefault方法,那么我有一个find和FindAll。而且我没有一个相等的顺序。我使用的是.NET3.5ah,我没有导入system.linq。。。不间断电源!
new string[3] { "GB", "United Kingdom", "United Kingdom" }?
// this returns the index for the matched array, if no suitable array found, return -1

public static intFindIndex(List<string[]> allString, string[] string)
{
    return allString.FindIndex(pt=>IsStringEqual(pt, string));
}


 private static bool IsStringEqual(string[] str1, string[] str2)
{
   if(str1.Length!=str2.Length)
      return false;
   // do element by element comparison here
   for(int i=0; i< str1.Length; i++)
   {
      if(str1[i]!=str2[i])
         return false;
   }
   return true;
}
return countryList.FirstOrDefault(array => array.SequenceEqual(arrayToCompare));