0

I have this argument from a function

params Expression<Func<Customer, object>>[] includes

I want to check if a property is defined in one of the includes and remove it. Something like this:

includes.ToList().Remove(x => x.Addresses)
h3n
  • 5,142
  • 9
  • 46
  • 76
  • What’s the issue? – Vivek Nuna Oct 11 '21 at 15:33
  • If you just want to check if two expressions are equal, you could use [this answer](https://stackoverflow.com/a/24528357/2791540). – John Wu Oct 11 '21 at 15:35
  • @viveknuna, I simply wanted to remove an item from the includes. But the above examples doesn't work.. – h3n Oct 11 '21 at 15:38
  • Does this answer your question? [ICollection.ToList().Insert doesn't insert value](https://stackoverflow.com/questions/65795700/icollection-tolist-insert-doesnt-insert-value) Basically, `ToList` is creating a new list, you'd need to instead create a new array without that value, and assign it to `includes` – Charlieface Oct 11 '21 at 15:42
  • @h3n have you tried setting Addresses as null? – Vivek Nuna Oct 11 '21 at 15:44
  • Note also that `x => x.Addresses` might not be the same as what was passed in. You may need something like `includes = includes.Where(e => e != new Expression>(x => x.Addresses)).ToArray();` – Charlieface Oct 11 '21 at 15:44
  • You can do `((MemberExpression)include.Body).Member.Name == "Addresses"` (some checks that expression is indeed simple `MemberExpression` won't hurt of course). – Evk Oct 11 '21 at 15:47

0 Answers0