2012-11-08 47 views
0

我時,我想下面的代碼asp.net - 指定的轉換無效的LINQ

IEnumerable<DataRow> projjType = from projT in dtProjList.AsEnumerable() 
           where projT.Field<int?>("ProjectId") == projId && 
           projT.Field<int?>("FilterId") == 1 
           select projT; 

和錯誤的堆棧跟蹤

at System.Data.DataRowExtensions.UnboxT`1.NullableField[TElem](Object value) 
    at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName) 
    at Folder.Projects.<>c__DisplayClass28.<repProjectList_ItemDataBound>b__1e(DataRow projT) in D:\Folder\Project.aspx.cs:line 1061 
    at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext() 
    at System.Linq.SystemCore_EnumerableDebugView`1.get_Items() 

其中dtProjList是DataTable的這個錯誤,和表結構如下,

ProjectId (int) 
ProjectName (string) 
ProjectThumb (string) 
FilterTypeId (int) 
FilterId (int) 
FilterType (string) 

所有不是空值。可以任何人請幫助我。

在此先感謝。

更新 ::這只是在我的表中記錄,是因爲單個記錄的錯誤??

ProjectId ProjectName ProjectThumb FilterTypeId FilterId FilterType 
    1 Datamaster small_14.jpg   1    1    Final 
+0

什麼是'projId'的類型(和值)? –

回答

4

如果所有字段不爲空,然後抹上領域int,而不是可空int?

var projjType = from projT in dtProjList.AsEnumerable() 
       where projT.Field<int>("ProjectId") == projId && 
         projT.Field<int>("FilterId") == 1 
       select projT; 
+0

謝謝@lazyberezovsky快速回復,但我也試過這也。它仍然無法正常工作。 –

+0

@ gofor.net什麼不起作用? BTW驗證'projId'變量的類型是'int'。 –

+0

@ gofor.net查詢將適用於任意數量的行。 –