2014-01-15 29 views
3

完整的錯誤消息:。任何導致演員到值類型「布爾」失敗,因爲物化值爲null

演員到值類型「布爾」失敗,因爲物化值爲null。結果類型的泛型參數或查詢都必須使用可爲空的類型。

代碼:

var messages = ctx.tblMessageQueue.Where(o => o.Status == status && o.Region == region);  
//o.Status is a byte type and not nullable, o.Region is an int type and not nullable. 

if(messages != null && messages.Any()) => Triggers the Error 
{... 
} 

堆棧跟蹤是:

在 System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader 1.GetValue(DbDataReader reader, Int32 ordinal) at lambda_method(Closure , Shaper) at System.Data.Common.Internal.Materialization.Coordinator 1.ReadNextElement(整形 整形器)在 System.Data.Common.Internal.Materialization.Shaper 1.SimpleEnumerator.MoveNext() at System.Linq.Enumerable.Single[TSource](IEnumerable 1 source)at System.Data.Objects.ELinq.ObjectQueryP rovider.b__3 [TResult(IEnumerable的查詢,表達queryRoot)在 在System.Linq.Queryable.Any [TSource(IQueryable`1源)

+1

確保使用LINQ提供者標記問題。如果您提出任何問題, – user2864740

+1

@ user3196525會迴應。 – PawanS

回答

0

我認爲你需要沿東西線這個:

var messages = ctx.tblMessageQueue.Where(o => o.Status == status && o.Region == region) 
            .Where(o => o.Status != null); 

我不確定的語法,但你只需要確保空狀態消息不會使它成爲新的變量。

這裏是一個例子/類似的問題 - Multiple Where clauses in Lambda expressions

0

檢查,看看你的留言表的特性的任何其他人空值,如果您的留言表包含空整數,那麼你應該考慮改變你的消息實體類「詮釋?」類型。

1

也許tblMessageQueue表中的一列值爲NULL,而其實體類中的相應屬性類型爲bool。如果是這樣的話,這裏有兩個建議,我能想到的:數據和表

  1. 正確,以便它不包含或允許NULL
  2. 更新實體類屬性可爲空:bool?
相關問題