2015-07-02 158 views
3

我想返回當前處於給定狀態之一的票證列表。有一個枚舉TicketState的數組(值爲Open,InProgress和Finished)。Linq包含枚舉數組

無法比較類型 'Project.BL.Domain.Ticketing.TicketState []' 的元素:

public IEnumerable<Ticket> ReadTickets(TicketState[] states) 
{ 
    return ctx.Tickets.Where(t => states.Contains(t.State)).AsEnumerable(); 
} 

以下異常當我測試方法出現。只支持原始類型,枚舉類型和實體類型。

我試着從數組中創建一個列表,並使用一個字節數組來替代,但我不斷收到異常。

有誰知道我該如何解決這個問題?

+1

該代碼甚至不會編譯 - 「states.Contains(t.State)'不是委託或表達式樹類型...現在我們可以猜測你剛剛錯過了'國家=>'但誰知道還有什麼其他的變化... –

+0

http://stackoverflow.com/questions/16615803/only-primitive-types-or-enumeration-types-are-supported-in-this-context ,http://stackoverflow.com/questions/15211362/only-primitive-types-or-enumeration-types-are-supported-in-this-context,http://stackoverflow.com/questions/15592042/linq-to -entities-only-primitive-types-or-enumeration-types-are-supported-error,http://stackoverflow.com/questions/7379394/entity-framework-unable-to-create-a-constant-value-of類型異常,請嘗試搜索。 EF無法將該LINQ轉換爲SQL查詢。哪個EF版本是這個?如何聲明Ticket.State? – CodeCaster

+0

@JonSkeet,'Contains'肯定會收到一個'T'並檢查它的存在。不需要委託或表達。 – haim770

回答

3

您是否在尋找Enumerable.Any

return ctx.Tickets.Where(t => states.Any(s => t.State == s)).AsEnumerable(); 
+0

謝謝,現在問題解決了:) – Jens