简体   繁体   English

Linq-查询列表<string[]>

[英]Linq - Query a List<string[]>

How do you query a List<string[]> to get the index of the arrays having matches on their sub-arrays and get a return of type System.Collections.Generic.IEnumerable<string[]> ? 如何查询List<string[]>以获得在其子数组上具有匹配项的数组的索引,并获取System.Collections.Generic.IEnumerable<string[]>类型的返回值?

EDIT: 编辑:

I have this: 我有这个:

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

and I what to get the the mylist indexes where rows[5] == "foo" 我该如何获取mylist索引,其中row [5] ==“ foo”

For the original question: 对于原始问题:

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

For the updated one: 对于更新的:

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

You do actually need to check if the array has at least 6 items as well: 您实际上确实需要检查数组是否也至少包含6个项目:

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

You mean something like this? 你的意思是这样的吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM