我生成使用LINQ表達一個where
條件。LINQ表達AndAlso與空類型
我的實體如下;
public class Sample
{
public int Id { get; set; }
public string Name { get; set; }
public int AnotherId { get; set; }
public int? RelationId { get; set; }
}
我不得不篩選基於2個鍵,即AnotherId
和RelationId.RelationId
(可選)的數據。所以在我的方法參數relationId
可能不會更新和爲0
在此基礎上,我需要生成一個表達式:
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);
}
在這裏我得到了在AndAlso
聲明以下異常:
二進制運算符AndAlso未對該類型 'System.Func``2 [樣品,System.Boolean]' 和 'System.Func`2 [樣品,System.Boolean]' 限定。
請幫我糾正我的問題。
檢查[此](http://stackoverflow.com/a/13968172/1559611)鏈路 –
我認爲'條件= Expression.Lambda> (Expression.AndAlso(condition.Body,additionalCondition.Body),condition.Parameters);!'應該工作 –
難道只是'x.AnotherId == anotherId &&(relationId> 0 || x.RelationId == relationId)'? –