2

I have an IQueryable object sth like;

IQueryable<People> query = DemoData.Where(x => true);

query = query.Where(p=> p.Nation == Nation); // query indicates people from a Nation now.

/* 
.
.
some code
.
.
*/

query = query.RemoveWhere(p=> p.Nation == Nation); // this is what I want to have
// query indicates all Nation now.

I do want to be able to remove a condition from the query. I tried to express above. Is that possible?

Ali CAKIL
  • 383
  • 5
  • 21
  • While it is possible, for which purpose it is needed? – Svyatoslav Danyliv Oct 25 '22 at 15:45
  • I receive this object as including some conditions already, I need to remove some conditions, an it is kindda dynamic. I do wonder, is it possible to remove any condition (or have a new instance without that condition)? – Ali CAKIL Oct 25 '22 at 15:57
  • It is EF Core queryable? – Svyatoslav Danyliv Oct 25 '22 at 16:27
  • [This](https://stackoverflow.com/a/10592424/861716) should do it with some minor changes. – Gert Arnold Oct 28 '22 at 06:52
  • 1
    @GertArnold, not minor. At least `ExpressionEqualityComparer` from EF Core will be needed, otherwise it is hard to compare expressions. – Svyatoslav Danyliv Oct 28 '22 at 09:15
  • @Svyatoslav I did try that modified code with EF-core 6, with success, but I must admit I don't fully understand the visitor logic because I don't exactly know how a `node` is constructed. That's why I don't post it as an answer. But AFAIKS there's no expression comparison involved, it's just that a node is removed from the tree. Of course I could easily have missed something. – Gert Arnold Oct 28 '22 at 10:56
  • 1
    @GertArnold, it is needed to find `p => p.Nation == Nation` pattern in expression tree and accurately replace with `True` constant. – Svyatoslav Danyliv Oct 28 '22 at 11:05
  • OK yes, if exactly *that* expression should be removed. I assumed it could work for the asker to simply remove the Where node(s) at the beginning of the code (forgot to mention that). But it doesn't look like it. @AliCAKIL? – Gert Arnold Oct 28 '22 at 11:20

0 Answers0