Accepted answer

You got really close.

Your idExpression is an Expression in the form of x => x.Property. However, you're passing the whole expression to the Equal expression. Change that to pass only the body:

var idEqualExpression = Expression.Equal(idSelector.Body, selectedValueConstant);

Then you can compile the lambda and pass it to FirstOrDefault without casting to a queryable:

var selectedSourceItem = source.FirstOrDefault(lambda.Compile());

Related Articles