在linq查询中使用string.IsnullorEmpty的问题

时间:2011-05-04 10:00:52

标签: c# linq

我有一个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)== false 更改为 pstmt.vcr_MetaValue!= string.Empty 它会给我 SQL Server不会处理NText,Text,Xml或Image数据类型的比较错误

5 个答案:

答案 0 :(得分:5)

嗯,错误信息似乎相当清楚 - 我怀疑如果您希望能够执行此操作,则需要使用nvarchar字段而不是text / {{1} }。

编辑:不仅数据库字段需要是正确的类型;它也是LINQ to SQL认为的类型。您需要使DBML与实际的数据库架构保持同步。

答案 1 :(得分:2)

您是否尝试过更换

where string.IsNullOrEmpty(pstmt.vcr_MetaValue)

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

答案 2 :(得分: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

答案 3 :(得分:1)

如果DB字段是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

答案 4 :(得分:0)

我不确定您使用的是哪个linq提供商,而且从快速谷歌搜索来看,IsNullOrEmpty似乎并未得到普遍支持。输入时弹出的答案看起来是正确的。