2013-06-21 21 views
2

如何選擇NULL作爲LINQ列選項。如何「選擇null作爲‘列’在C#中使用LINQ

我的SQL會讀。

Select field1, field2, field3, null as "null", field4 
from table1 

我的LINQ查詢會:

from t in table1 
select new { t.field1, t.field2, t.field3, null as "null", t.field4} 

由Visual Studio生成的錯誤是:

鈣n不是分配給匿名類型屬性

回答

5

刪除該as "null"並命名參數,鑄造null其類型:

from t in table1 
select new { t.field1, t.field2, t.field3, someField = (string)null, t.field4} 
+0

優秀,感謝您的。 – vwdewaal