我创建了一个数据类型为List的数组,如下所示:
List<string>[] phase2 = new List<string>[200];
有很多初始化(这是一个简单的示例):
phase2[0] = new List<string>() { "Bob", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes", };
phase2[1] = new List<string>() { "Jim", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes" };
phase2[2] = new List<string>() { "Joe", "Complex", "A", "AOT", "zNO", "Yes", "Yes", "Yes" };
phase2[3] = new List<string>() { "Bill", "Complex", "A", "AOT", "Yes", "Yes", "Yes", "Yes" };
phase2[4] = new List<string>() { "Robert", "Complex", "A", "PL", "zNO", "Yes", "Yes", "Yes" };
我想知道如何在索引3中搜索已初始化的所有数组中的字符串“ B”?
还有一个好处(不确定是否可行),您可以在DIFFERENT索引中搜索MULTIPLE字符串吗?例如,我想返回所有在索引3中具有字符串值“ B”,在索引5中具有字符串值“是”的列表。
任何帮助将不胜感激。
编辑:感谢您的所有答复!我还没有了解linq,所以很多答案都在我头上。感谢lollercoasters提供的愚蠢解决方案:)
答案 0 :(得分:2)
如果您希望更好地理解非Linq lambda解决方案,请在此处进行配合:
List<string>[] phase2 = new List<string>[200];
phase2[0] = new List<string>() { "Bob", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes", };
phase2[1] = new List<string>() { "Jim", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes" };
phase2[2] = new List<string>() { "Joe", "Complex", "A", "AOT", "zNO", "Yes", "Yes", "Yes" };
phase2[3] = new List<string>() { "Bill", "Complex", "A", "AOT", "Yes", "Yes", "Yes", "Yes" };
phase2[4] = new List<string>() { "Robert", "Complex", "A", "PL", "zNO", "Yes", "Yes", "Yes" };
for (int i = 0; i < phase2.Length; i++)
{
var list = phase2[i];
if (list == null) continue; // skip empty lists
if (list[2] == "B" && list[4] == "Yes")
{
// found elements in list
Console.WriteLine("found elements in list at index: " + i);
}
}
答案 1 :(得分:1)
这是LINQ的实现方法:
static void Main(string[] args)
{
List<string>[] phase2 = Enumerable.Repeat(new List<string>(), 200).ToArray();
phase2[0] = new List<string>() { "Bob", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes", };
phase2[1] = new List<string>() { "Jim", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes" };
phase2[2] = new List<string>() { "Joe", "Complex", "A", "AOT", "zNO", "Yes", "Yes", "Yes" };
phase2[3] = new List<string>() { "Bill", "Complex", "A", "AOT", "Yes", "Yes", "Yes", "Yes" };
phase2[4] = new List<string>() { "Robert", "Complex", "A", "PL", "zNO", "Yes", "Yes", "Yes" };
List<string>[] result = phase2?.Where(x => x.Any(y => y == "B")).ToArray();
}
答案 2 :(得分:0)
简而言之,请记住,阶段2中的每个元素都是一个列表,因此它实现了Enumerable,因此打开了所有LINQ功能。 使用您提供的功能(谓词),立即就会想到查找。
如果您研究此途径,将会学到很多东西。
答案 3 :(得分:0)
您可以使用Linq搜索具有与您的参数匹配的元素的所有列表,如下所示:
phase2.Where(list => list.Any(item => item == "string to search for"));
答案 4 :(得分:0)
以下是可以帮助您的课程:
public class ListFilterer
{
private readonly List<string>[] _searchSpace;
public ListFilterer(List<string>[] searchSpace)
=> this._searchSpace = searchSpace ?? new List<string>[0];
public IEnumerable<List<string>> GetListsHaving(params Tuple<int, string>[] query)
=> (from l in _searchSpace
where AllConditionsMatch(l, query)
select l);
private static bool AllConditionsMatch(IReadOnlyList<string> lst, IEnumerable<Tuple<int, string>> conditions)
{
foreach (var cond in conditions)
{
if (cond.Item1 < 0 || cond.Item1 > lst.Count - 1)
return false;
if (lst[cond.Item1] != cond.Item2)
return false;
}
return true;
}
}