I am generating one where
condition using a LINQ expression.
My entity is as follows;
public class Sample
{
public int Id { get; set; }
public string Name { get; set; }
public int AnotherId { get; set; }
public int? RelationId { get; set; }
}
I have to filter data based on 2 keys, namely AnotherId
and RelationId.RelationId
(optional). So in my method parameter relationId
may not update and be 0.
Based on this I need to generate an expression:
Expression<Func<Sample, bool>> condition = x => x.AnotherId == anotherId;
if (relationId > 0)
{
Expression<Func<Sample, bool>> additionalCondition = x => x.RelationId == relationId;
condition = Expression.Lambda<Func<Sample, bool>>(Expression.AndAlso(condition, additionalCondition), condition.Parameters);
}
Here I got the following Exception in the AndAlso
statement:
The binary operator AndAlso is not defined for the types 'System.Func``2[Sample,System.Boolean]' and 'System.Func`2[Sample,System.Boolean]'.
Please help me to correct my issue.