2011-05-04 156 views
3

我有一個linq查詢,其中我想包括那些非空或空的數據庫字段的記錄,但是當我使用string.isNullorEmpty它給了我錯誤。我怎樣才能實現這個任務,我的查詢是linq查詢中使用string.IsnullorEmpty的問題

from post in PostIdPostMeta1 
join pstmt in postrepository.GetAllPostMetas() on post.int_PostId equals pstmt.int_PostId 
where string.IsNullOrEmpty(pstmt.vcr_MetaValue) == false 
select post 

如果我改變string.IsNullOrEmpty(pstmt.vcr_MetaValue)==假pstmt.vcr_MetaValue!=的String.Empty它給我SQL Server會不處理NTEXT的比較,文本,XML或圖像數據類型錯誤

+1

什麼是錯誤? – Aliostad 2011-05-04 10:02:25

+0

布爾IsNullOrEmpty(System.String)'沒有支持轉換爲SQL – 2011-05-04 10:03:54

回答

5

那麼,錯誤消息似乎相當明確的 - 我懷疑,如果你希望能夠做到這一點,你需要使用的,而不是text/ntextnvarchar場。

編輯:這不只是一個需要正確類型的數據庫字段;它也是LINQ to SQL認爲它的類型。你需要保持你的DBML同步您的實際數據庫模式。

+0

字段已經是數據庫中的nvarchar – 2011-05-04 10:39:39

+0

@Fraz:你絕對*確定*?錯誤消息強烈表明它不是。在某些早些時候是文本/文字嗎?也許你需要更新LINQ to SQL實體... – 2011-05-04 10:40:56

+0

是的我的錯誤,我忘記更改數據庫中的數據類型後更新DBML – 2011-05-04 10:56:17

1

試試這個代碼:

from post in PostIdPostMeta1 
join pstmt in postrepository.GetAllPostMetas() on post.int_PostId equals pstmt.int_PostId 
where ((pstmt.vcr_MetaValue != "") && (pstmt.vcr_MetaValue != null)) 
select post 
1

你試過repla慶安

where string.IsNullOrEmpty(pstmt.vcr_MetaValue) 

where pstmt.vcr_MetaValue != null && pstmt.vcr_MetaValue != string.Empty 

+0

它給我SQL Server不處理NText,Text,Xml或圖像數據類型的比較錯誤 – 2011-05-04 10:32:13

0

我不知道你所使用的LINQ提供程序,並從快速谷歌,荷蘭國際集團似乎IsNullOrEmpty是沒有得到普遍支持。輸入時彈出的答案看起來正確。

1

如果數據庫字段是NTEXT,您可以檢查是否領域並不像空字符串。這是一個例子。

from post in PostIdPostMeta1 
join pstmt in postrepository.GetAllPostMetas() on post.int_PostId equals pstmt.int_PostId 
where !SqlMethods.Like(pstmt.vcr_MetaValue, "") 
select post 
+0

就像方法不檢查空字段,所以我已經移除例如空檢查。 – 2013-11-20 11:01:44