需要一个linq查询,其中包含指示属性匹配辅助列表中的所有值

时间:2011-02-18 19:09:47

标签: c# .net linq datatable

我想在一个查询中复制以下逻辑。

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对于任何整个Word都是真的,那么它会导致匹配,而不是我的意图(匹配意味着!对于excludeWholeWords中的所有项目,FoundWholeWord都为真

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

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

这个怎么样?

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

答案 1 :(得分: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 :(得分:0)

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

这就是ReSharper的“转换为LINQ表达式”所做的。