tags:

views:

58

answers:

2

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"

+5  A: 

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"
sukru
I think you mean `Where` instead of `Select`. The code you've posted will return an `IEnumerable<bool>`.
Dan Tao
Thanks, I mixed with SQL syntax...
sukru
@sukru: Happens to the best of us ;)
Dan Tao
I've edited my question with some code, but how does this return an index ?
metro
Added another solution for the updated version of the problem.
sukru
+1 on Dan Tao's comment, you should do a .ToList() at the end if you need it to be in a List<T>.
Martin Ongtangco
+1  A: 

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