Hans Espen
Hans Espen

Reputation: 1127

How to use Expression.IfThenElse in C#

I have a method in a repository:

Query<T>(System.Linq.Expressions.Expression<Func<T,bool>> function)

When i call it, I want to use different Expressions based on a condition.

I tried:

m_Repository.Query<MyObject>(x => x.Infos.Count > 1 ? 
x.Infos.Any(y => y.Info.Name.StartsWith(s)) :
x.Name.StartsWith(s));

but then it always executes the else part. I figured I had to use Expression.IfThenElse, but I cannot make it work. Any suggestions on how to do this?

Thanks!

Upvotes: 2

Views: 2196

Answers (1)

Double Down
Double Down

Reputation: 908

Have you tried Expression.Condition

Upvotes: 3

Related Questions