Linq - 查询列表<string []> </string []>

时间:2010-07-28 21:43:16

标签: c# linq .net-3.5

如何查询List<string[]>以获取其子阵列上具有匹配项的数组的索引并获得System.Collections.Generic.IEnumerable<string[]>类型的返回值?

修改

我有这个:

       string[] report = File.ReadAllLines(@".\REPORT.TXT").AsQueryable().Where(s
       => s.StartsWith(".|")).ToArray();

      List<string[]> mylist = new List<string[]>();

        foreach (string line in report)
        {
            string[] rows = line.Split('|');
            mylist.Add(rows);
        }

我想得到行[5] ==“foo”

的mylist索引

2 个答案:

答案 0 :(得分:6)

对于原始问题:

list.Where(array => array.Any(item => item == match))

更新的:

result = Enumerable.Range(0, list.Count - 1).Where(i => list[i][5] == "foo");

您确实需要检查数组是否还包含至少6个项目:

i => list[i].Length > 5 && list[i][5] == "foo"

答案 1 :(得分:1)

你的意思是这样的吗?

var haystacks = new List<string[]>();

haystacks.Add(new string[] { "abc", "def", "ghi" });
haystacks.Add(new string[] { "abc", "ghi" });
haystacks.Add(new string[] { "def" });

string needle = "def";

var haystacksWithNeedle = haystacks
    .Where(haystack => Array.IndexOf(haystack, needle) != -1);