【轉】關於LinQ的動態Or查詢

來源:互聯網
上載者:User

標籤:

/// <summary>/// 建構函式使用True時:單個AND有效,多個AND有效;單個OR無效,多個OR無效;混合時寫在AND後的OR有效/// 建構函式使用False時:單個AND無效,多個AND無效;單個OR有效,多個OR有效;混合時寫在OR後面的AND有效/// </summary>public static class PredicateExtensions{    public static Expression<Func<T, bool>> True<T>() { return f => true; }    public static Expression<Func<T, bool>> False<T>() { return f => false; }    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expression1, Expression<Func<T, bool>> expression2)    {        var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());        return Expression.Lambda<Func<T, bool>>(Expression.Or(expression1.Body, invokedExpression), expression1.Parameters);    }    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expression1, Expression<Func<T, bool>> expression2)    {        var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());        return Expression.Lambda<Func<T, bool>>(Expression.And(expression1.Body, invokedExpression), expression1.Parameters);    }}

 

調用方法:

HUBDataDataContext db = new HUBDataDataContext();var predicate = PredicateExtensions.False <T_DISPATCH>();predicate = predicate.Or(c => c.ToHUBID == 1);var dispatch = db.T_DISPATCH.Where(predicate).Where(c => c.State == 3); 

 

轉自:http://www.cnblogs.com/ejiyuan/archive/2009/07/14/1523515.html

【轉】關於LinQ的動態Or查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.