2014-10-07 19 views
0

這是我的代碼指定的轉換是無效的例外

var result = (from row1 in table.AsEnumerable() 
          join row2 in tabelPopup.AsEnumerable() 
          on row1.Field<string>("CallID") equals 
          row2.Field<string>("callID") 
          where row1.Field<string>("Direction") == "I" 
          select new 
          { 
           Agent = row1.Field<string>("Agent"), 
           StartTime = row1.Field<DateTime>("StartTime"), 
           Reason = row2.Field<string>("Reason") 
          }); 

其中tabletablePopup是數據表變量。

我得到這個異常:

Specified cast is not valid 

這個代碼:

new 
          { 
           Agent = row1.Field<string>("Agent"), 
           StartTime = row1.Field<DateTime>("StartTime"), 
           Reason = row2.Field<string>("Reason") 
          } 
+0

您確定列定義正確嗎?即Agent是字符串,StartTime是日期時間,Reason是字符串? – artm 2014-10-07 10:43:48

+1

顯然,其中一個字段包含一個類型的值,這不是'Field '方法所期望的(假設它只是'(T)row [fieldName]')。 – Dennis 2014-10-07 10:45:55

+0

@artm你是對的,'datetime'應該是'string',請輸入一個答案以接受它 – 2014-10-07 10:46:43

回答

2

確保您的列定義與您在row1.field中使用的類型匹配<>。即Agent是字符串,StartTime是日期時間,Reason是字符串。這可能是由於StartTime不是日期時間類型。

1

大概StartTime是不是從DateTime類型。因爲你收到這個例外。嘗試轉換它。如果這是正確的,你應該將其轉換爲DateTime或者只是檢索字符串值。

+0

+1給你,但'artm'找到你之前的解決方案請原諒我請不要接受你的回答+1 – 2014-10-07 10:47:16

+0

@MarcoDinatsoli不用擔心:) – mybirthname 2014-10-07 10:48:00