的Linq-to-SQL首先,我必須說我已經在StackOverflow上閱讀了幾篇文章,但是我無法得到期望的結果。結合兩個Expression <Func <T, bool>>(「where clause」)和/或
讓我解釋上下文(簡化):我使用Linq-to-SQL來查詢最近訪問商店的客戶和(可選)只獲得具有一定支付金額的客戶。假設我有一個包含客戶端,訪問和付款類的模型。
因此,着眼於Where
表達,我想這樣的:
Expression<Func<Entityes.Clients, bool>> filter = null;
filter = c =>
c.Visits.Any(v => v.VisitDate > DateTime.Now.Date.AddMonths(-(int)visitsSince));
if (minPayment.HasValue && minPayment.Value > 0)
{
filter.And(
c => c.Payments.Sum(p => p.Quantity) > minPayment.Value);
}
的filter.And
方法是一個擴展方法,在這個論壇上建議,下面你可以看到的定義:
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expression1,
Expression<Func<T, bool>> expression2)
{
InvocationExpression invokedExpression =
Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>
(Expression.And(expression1.Body, invokedExpression), expression1.Parameters);
}
但是,這並不適用於我的預期,並且結果不會按付款金額過濾。有數據或LINQ到SQL模型中沒有錯誤,因爲該代碼工作正常:
filter = c =>
c.Visits.Any(v => v.VisitDate > DateTime.Now.Date.AddMonths(-(int)visitsSince)))
&& c => c.Payments.Sum(p => p.Quantity) > minPayment.Value;
不過,我不希望因爲我有一些更復雜的場景中,我需要使用的最後一個代碼使用每個部分的「和/或」組合逐步構建過濾器表達式。 PD:我是一個「ADO.Net DataSets」傢伙,試圖學習Linq-to-SQL和即將推出的Entity Framework,我希望能很快對StackOverflow社區有所幫助。
p.s.,您應該使用'Expression.AndAlso()'方法來生成邏輯AND表達式('&&')。 'Expression.And()'產生按位AND表達式('&')。 – 2012-02-21 22:01:31