开发者

Need a linq query where inclusion indicates property matches all values in secondary list

开发者 https://www.devze.com 2023-02-12 12:22 出处:网络
I want to duplicate the following logic in a single query. var currentRows = resultsTable.AsEnumerable();

I want to duplicate the following logic in a single query.

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

I had tried the following, but it results in matching if !FoundWholeWord is true for any wholeWord, instead of my intent (which is that a match means !FoundWholeWord is true for ALL items in excludeWholeWords

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

Any ideas?


If I understand the question correctly, it should be something along the lines of:

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

I don't know what the FoundWholeWord is but if it does anything different than just comparing strings, you can use it like:

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


How about this?

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


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

That's what ReSharper's "Convert to LINQ expression" does.

0

精彩评论

暂无评论...
验证码 换一张
取 消