2011-02-18 19 views
0

我想在單個查詢中複製以下邏輯。需要一個linq查詢,其中包含指示屬性與二級列表中的所有值匹配

var currentRows = resultsTable.AsEnumerable(); 
foreach (var wholeWord in excludeWholeWords) 
{ 
    currentRows = from row in currentRows 
     where !FoundWholeWord(wholeWord, row.Field<string>("busdescl")) 
     select row; 

} 
resultsTable = currentRows.CopyToDataTable(); 

我曾嘗試以下,但它導致了匹配,如果!FoundWholeWord是真實的任何全字,而不是我的意圖(這是一個比賽意味着什麼!FoundWholeWord是真的在excludeWholeWords所有項目

var matchGvRows = (from wholeWord in excludeWholeWords 
    from row in gvkeysTable.AsEnumerable() 
    where !FoundWholeWord(wholeWord, row.Field<string>("busdescl")) 
    select row).Distinct(); 

任何想法?

回答

2

如果我理解正確的問題,它應該是沿着線的東西:

var newRows = currentRows 
    .Where(r => !excludeWholeWords.Any(w => w == r.Field<string>("busdescl")); 

我不知道是什麼FoundWholeWord是,但如果這樣做有什麼不同不僅僅是比較字符串,你可以用它喜歡:

var newRows = currentRows 
    .Where(r => !excludeWholeWords.Any(w => FoundWholeWord(w, r.Field<string>("busdescl"))); 
2

這個怎麼樣?

var matchGvRows = excludeWholeWords.Aggregate(currentRows, (current, wholeWord) => current.Where(row => !FoundWholeWord(wholeWord, row.Field<string>("busdescl")))); 
+0

Aggregate按我的意圖工作,但需要比@Klaus的第二個示例長很多。 – rediVider 2011-02-18 19:47:34

0
currentRows = excludeWholeWords.Aggregate(currentRows, (current, wholeWord) => (from row in current 
                         where !FoundWholeWord(wholeWord, row.Field<string>("busdescl")) 
                         select row)); 

這就是ReSharper的 「轉換爲LINQ表達」 一樣。

相關問題