2013-08-21 57 views
1

我正在使用EF並希望通過WCF服務過濾功能。 如果我有一個過濾器,一切都可以,但是當我嘗試將一些表達式組合在一起時,我得到一個錯誤的錯誤: 底層連接已關閉:服務器關閉了預期保持活動狀態的連接。如何組合表達式

因此,這裏是我的Filter對象:

public class EventFilter 
{ 
    public int CategoryId { get; set; } 
    public int SubCategoryId { get; set; } 
    public DateTime StartDate { get; set; } 
    public int? Duration { get; set; } 
    public string Address { get; set; } 
} 

這裏是過濾功能工作正常(一個過濾器):

public IQueryable<Event> GetBySearch(EventFilter search) 
{ 
    Expression<Func<Event, bool>> where = null; 

    if (search.CategoryId != 0) 
    { 
     where = x => x.CategoryId == search.CategoryId; 
    } 

    return this.Context.Events.Where(where); 
} 

但是,如果我想擴大它的兩個過濾器,它不工作,我不明白如何解決它。

所以,這是組合功能,我相信這是正常的。

private static Expression<Func<TEntity, bool>> Combine<TEntity>(Expression<Func<TEntity, bool>> a, Expression<Func<TEntity, bool>> b) 
{ 
    var and = Expression.AndAlso(a.Body, b.Body); 
    var param = Expression.Parameter(typeof(TEntity)); 

    return Expression.Lambda<Func<TEntity, bool>>(and, param); ; 
} 

而現在的問題是過濾器功能(當兩個濾波器發生它不工作:

public IQueryable<Event> GetBySearch(EventFilter search) 
{ 
    Expression<Func<Event, bool>> where = null; 

    if (search.CategoryId != 0) 
    { 
     where = x => x.CategoryId == search.CategoryId; 
    } 

    if (search.SubCategoryId != 0) 
    { 
     where = Combine<Event>(where, x => x.SubCategoryId == search.SubCategoryId);; 
    } 

    return this.Context.Events.Where(where); 
} 

我已經嘗試了許多不同的寫吧,我試圖進入一個新的對象到結果,例如:

public IQueryable<Event> GetBySearch(EventFilter search) 
{ 
    Expression<Func<Event, bool>> where = null; 
    Expression<Func<Event, bool>> where2 = null; 

    if (search.CategoryId != 0) 
    { 
     where = x => x.CategoryId == search.CategoryId; 
    } 

    if (search.SubCategoryId != 0) 
    { 
     where2 = x => x.SubCategoryId == search.SubCategoryId; 
    } 

    var result = Combine(where, where2); 

    return this.Context.Events.Where(result); 
} 

畢竟我所許的注意,此代碼:

Expression<Func<Event, bool>> where3 = (x => x.CategoryId == search.CategoryId) && (x => x.SubCategoryId == search.SubCategoryId); 

雖然這不是:

Expression<Func<Event, bool>> where3 = where && where2; //Compile time error. 

也許這裏的問題開始,有人可以幫我嗎? 謝謝!

+0

米什你能包括你所得到的編譯器錯誤?我還建議您使用「where」作爲變量的名稱以獲取更多詳細信息,請參閱此鏈接http://msdn.microsoft.com/en-us/library/x53a06bb.aspx –

回答

2

因爲多個Where子句是AND合在一起,並且每個子句返回一個IQueryable<Event>您可以像這樣鏈接多個子句。

public IQueryable<Event> GetBySearch(EventFilter search) 
{ 
    IQueryable<Event> events = this.Context.Events; //(I assume Events is an IQueryable<Event>) 

    if (search.CategoryId != 0) 
    { 
     events = events.Where(x => x.CategoryId == search.CategoryId); 
    } 

    if (search.SubCategoryId != 0) 
    { 
     events = events.Where(x => x.SubCategoryId == search.SubCategoryId); 
    } 

    return events; 
} 

這實際上不會(假設這兩個ID在那裏不爲0)

this.Context.Events.Where(x => x.CategoryId == search.CategoryId).Where(x => x.SubCategoryId == search.SubCategoryId); 

這是一樣的

this.Context.Events.Where(x => x.CategoryId == search.CategoryId && x.SubCategoryId == search.SubCategoryId); 
+0

非常感謝您,它現在可以工作! –