使用字符串时出现问题.linq查询IsnullorEmpty

本文关键字:问题 linq 查询 IsnullorEmpty 字符串 | 更新日期: 2023-09-27 17:54:30

我有一个linq查询,我想包括那些记录不是空或空在数据库字段,但当我使用字符串。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) == falsepstmt. vcr_metavalue。= string. vcr_MetaValue !空它给我SQL Server不处理NText, Text, Xml或Image数据类型的比较错误

使用字符串时出现问题.linq查询IsnullorEmpty

嗯,错误消息似乎相当清楚-我怀疑,如果您想能够做到这一点,您需要使用nvarchar字段而不是text/ntext

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

有没有试过替换

where string.IsNullOrEmpty(pstmt.vcr_MetaValue)

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

?

试试下面的代码:

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

如果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

我不确定您正在使用哪个linq提供程序,并且从快速的谷歌搜索中似乎不普遍支持IsNullOrEmpty。输入这句话时弹出的答案看起来是正确的。