1

I have list new List<Expression<Func<Test, bool>>>

and I'd want to add item to with all operations in .Add() scope

How can I achieve that?

var a = new List<Func<Test, bool>>();

var b = new List<Expression<Func<Test, bool>>>();

a.Add
(
    new Func<Test, bool>(x => x.test == false) // works
);

b.Add
(
    new Expression<Func<Test, bool>>(x => x.test == false) // fails
);
Joelty
  • 1,751
  • 5
  • 22
  • 64
  • Look at this [Maybe you find your answer here](https://stackoverflow.com/questions/673205/how-to-check-if-two-expressionfunct-bool-are-the-same) –  Nov 09 '18 at 09:56
  • 1
    Your code doesn't compile because you can't declare an expression that way -> that constructor does not exist. Simply passing ` x => x.test == false` works. – thisextendsthat Nov 09 '18 at 09:56

1 Answers1

3

Just omit the type and let the compiler infer it:

var a = new List<Func<Test, bool>>();

var b = new List<Expression<Func<Test, bool>>>();

a.Add
(
    x => x.test == false
);

b.Add
(
    x => x.test == false 
);
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111