简体   繁体   中英

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[]> ?

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"

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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