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 r开发者_开发百科eport)
{
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);
精彩评论