2013-03-12 55 views
5

這是我的代碼:實體框架5.0。我的查詢有什麼問題?

public DateTime GibSomeStartDate(IEnumerable<int> partnerNumbers, DateTime startTime) 
{ 
    var contractsStartDate = from contract in this.databaseContext.Contract 
           where partnerNumbers.Contains(contract.Pnr) 
           && contract.SomeDateTime >= startTime 
           select contract.SomeDateTime; 
} 

如果我叫contractsStartDate.Min()發生異常:

Unable to create a null constant value of type 'System.Collections.Generic.IEnumerable`1'. Only entity types, enumeration types or primitive types are supported in this context. 

什麼是錯我的查詢?

  • contractsStartDate的類型是 System.Data.Entity.Infrastructure.DbQuery

  • EF的5.0

  • databaseContextSystem.Data.Entity.DbContext

+1

@GalacticCowboy - JustAnotherUserYouMayKnow有嚴格的答案)它是空:(謝謝!) – MikroDel 2013-03-12 14:18:18

回答

3

我知道這個錯誤的孩子。只要確保partnerNumbers不爲空即可。您爲此參數傳遞一個空值,但Linq-to-entities無法將該值轉換爲任何有意義的值。

if (partnerNumbers == null) 
{ 
    throw new ArgumentNullException("partnerNumbers"); 
} 

一個額外的獎金的建議:

如果SomeDateTimenot nullable,並有在你的枚舉沒有條目,那麼你會在調用Min()得到一個異常。在您的查詢中將SomeDateTime鑄造爲nullable類型將會起作用,然後在沒有條目時您會得到空值。

+0

謝謝 - 我現在檢查你的建議 – MikroDel 2013-03-12 14:08:58

+0

爲了將來的參考,線索在例外:LINQ/EF查詢返回一個' IQueryable',而不是'IEnumerable',所以這個參數是範圍內唯一明顯的'IEnumerable'。 :)(從技術上講,'IQueryable'也是一個'IEnumerable',但是錯誤將顯示最具體的類型。) – GalacticCowboy 2013-03-12 14:25:50

+0

@GalacticCowboy - 請在評論中使用「@」 - 比如「@GalacticCowboys」 - 比生病了解新信息。感謝您使用IQueryable和IEnumerable。但我還沒有明白這個「線索」:) – MikroDel 2013-03-12 14:36:53