代码之家  ›  专栏  ›  技术社区  ›  Hassen

linq-query a list<string[]>

  •  1
  • Hassen  · 技术社区  · 14 年前

    如何查询 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);
            }
    

    我怎么得到mylist索引,其中rows[5]=“foo”

    2 回复  |  直到 14 年前
        1
  •  6
  •   sukru    14 年前

    对于原始问题:

    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"
    
        2
  •  1
  •   Dan Tao    14 年前

    你的意思是这样的?

    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);