2010-07-28 48 views
1

如何查詢List<string[]>以獲取在其子陣列上匹配的數組的索引並獲得類型System.Collections.Generic.IEnumerable<string[]>的返回值?Linq - 查詢列表<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指標中行[5] == 「foo」 的

+0

嗯......什麼? :)你能澄清你的意思嗎?「在他們的子陣列上有匹配」?你的意思是,在父級List <>中的字符串[]的索引是否與該字符串[]中的元素數目匹配? – 2010-07-28 21:48:07

+0

構建List的代碼示例如何,以便我們可以更好地查看您想要的輸出。 – mpenrow 2010-07-28 21:49:54

+0

@Matt Peterson @mpenrow,好的,這裏有些代碼 – Hassen 2010-07-28 22:11:27

回答

6

對於原來的問題:

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" 
+0

我認爲你的意思是'Where'而不是'Select'。您發佈的代碼將返回一個'IEnumerable '。 – 2010-07-28 21:49:39

+0

謝謝,我與SQL語法混合... – sukru 2010-07-28 21:50:44

+1

@sukru:發生在我們最好的;) – 2010-07-28 21:51:48

1

你的意思是這樣的嗎?

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