I want to do myObjectList.Where(x => x.PropertyAsParameter)
This way I can run the method for each property I want to search for by passing in a string that replaces PropertyAsParameter with the property name.


I seen some reference to reflection but was unsure if this would be the direction I’d have to go or if there was another way I’m not seeing. Thanks.
Yeah, basically you have to build a LINQ expression using LINQ.Expressions.
x => x.PropertyAsParameteris essentially a Lambda function where x is the argument, and has the type of the element of myObjectList, and the function returns a bool
The equivalent of that would be, assuming that PropertyAsParameter is a boolean:

There are a couple of things going on here. BuildExpression builds your LINQ Expression object graph thing of it as writing code as a data structure.
first we create a ParameterExpression. It is an expression that stands in for the parameter “x” in our lambda. It is of course, type T, where T is whatever type we passed to the generic method.
Then we need to build the body of the function, which is basically:
In Intermediate language (IL), the last value on the stack is the return value. so the body of the function here is just the propertyExpression which we assume is of type bool.


propertyExpression is a LINQ Expression that accesses a property on they type we specify, on the instance we supply. The instance is the parameterExpression, and the type is T, the type of the parameter expression.
Finally we create the lamda expression itself.
Now, this is just an Expression, a code construct. It does not actually execute. There are two ways to use this. As-is, an Expression<Func<T, bool>> can only be used in a Queryable. If you want to use in an Enumerable, you need to Compile it first. Then it becomes a Func<T, bool> that you can pass as an argument into a Where clause, which is what the Where extension methods do.
Finally, to use them you can do something like:

Of course, you might have much more complex logic, requiring a more complex expression tree to be built in BuildExpression. For example the property might not be a boolean and you might want to compare it with something else. You should construct the appropriate expression tree using LINQ Expressions. To perform an Equals such as x.PropertyName == “Hello” you might write:


Just remember that a Where always accepts a Func<T, bool>
Thanks for responding in a very detailed manner. I’ll check this out when I get time. This most certainly seems more intricate than what I expected. However, I guess this is a good point to learn something new. Reflection is probably something to learn sooner rather than later.
What’s the actual use case of this? Sounds complicated. Can’t you make the method a generic with a base class or interface containing common properties, and then pass in a predicate?


It’s so the code can be more concise and I can easily return the results I need for this specific use case. I’m sure there are a million ways to do it differently.
I have to aggregate a list of objects (with 11 fields) and count them based on their property values to display them in a graph. So if I can have 1 method that’s dynamic instead of a method for each property I have to iterate through that would be great.


Yes, reflection. But that’s only half the battle.
After using reflection to find the MyObject property that matches the specified (string) name, you have to construct the desired lambda expression for myObjectList using the property metadata.
It gets deep so I’m not going to try to lay it out for you here — just Google/YouTube for how to manually construct a lambda expression .

source