3

I have dynamic string generated from back end which generates expression like:

"Col NOT IN ('ABC','CDE','EDF'...)"

I would like to make use of above string has predicate to my IEnumerable collection. Does Dynamic LINQ supports NOT IN operation?

Yared
  • 2,206
  • 1
  • 21
  • 30
user145610
  • 2,949
  • 4
  • 43
  • 75
  • 1
    I have assumed the `ABC` `CDE` and `EDF` values are contained in a string array i.e. `notInArray = ["ABC", "CDE", "EDF"]`. Could you not simply do `collection = collection.Where(i => !notInArray.Contains(i.Col))`? – nbokmans Jan 02 '18 at 12:39
  • `IN` is `Contains` in LINQ: https://stackoverflow.com/q/15633066/861716 – Gert Arnold Jan 02 '18 at 12:49
  • It's not supported directly (in fact Dynamic LINQ supports just a limited set of standard LINQ methods). You have to do parsing yourself. Or modify the backend to generate some more structured query definition instead of `string`. – Ivan Stoev Jan 03 '18 at 13:33

2 Answers2

1

It is possible to do this by using a SQL syntax that is much less common, but does the same thing:

"NOT (Col IN ('ABC','CDE','EDF'...))"

This works both for SQL and Dynamic Linq

arhnee
  • 1,044
  • 9
  • 24
-1

You didn't provide your sample code but it seems something like;

var array = new [] {"ABD", "ABC", "BED"};
var result = _context.table.Where(x => !array.Any(y => x.Col1 == y));
lucky
  • 12,734
  • 4
  • 24
  • 46