简体   繁体   English

在 linq 查询中使用 string.IsnullorEmpty 时出现问题

[英]Problem using string.IsnullorEmpty in linq query

I have a linq query in which i want to include those record which are not null or empty in database field but when i use string.isNullorEmpty it gives me error.我有一个 linq 查询,我想在其中包含那些不是 null 或在数据库字段中为空的记录,但是当我使用 string.isNullorEmpty 时它给了我错误。 How can i achieve this task my query is我怎样才能完成这个任务我的查询是

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

If i change string.IsNullOrEmpty(pstmt.vcr_MetaValue) == false to pstmt.vcr_MetaValue.= string.Empty it give me SQL Server does not handle comparison of NText, Text, Xml, or Image data types error如果我将string.IsNullOrEmpty(pstmt.vcr_MetaValue) == false更改为pstmt.vcr_MetaValue.= string.Empty它会给我SQL 服务器不处理 NText、Text、Xml 或图像数据类型的比较错误

Well, the error message seems reasonably clear - I suspect that if you want to be able to do this, you'll need to use an nvarchar field instead of text / ntext .好吧,错误消息似乎相当清楚-我怀疑如果您希望能够执行此操作,则需要使用nvarchar字段而不是text / ntext

EDIT: It's not just the database field that needs to be the right type;编辑:不仅仅是数据库字段需要是正确的类型; it's also the type that LINQ to SQL thinks it is.也是LINQ到SQL认为的类型。 You need to keep your DBML in sync with your actual database schema.您需要使您的 DBML 与您的实际数据库模式保持同步。

Have you tried replacing您是否尝试过更换

where string.IsNullOrEmpty(pstmt.vcr_MetaValue)

with

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

? ?

If DB field is NTEXT, You can check if field is not like empty string.如果 DB 字段是 NTEXT,您可以检查字段是否不像空字符串。 Here's an example;这是一个例子;

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

Try this code:试试这个代码:

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

I'm not sure about which linq provider you are using, and from a quick google-ing it seems that IsNullOrEmpty is not universally supported.我不确定您使用的是哪个 linq 提供程序,并且从快速谷歌搜索来看,似乎没有普遍支持 IsNullOrEmpty。 The answers that popped up while typing this look correct.键入此内容时弹出的答案看起来是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM