Problem using string.IsnullorEmpty in linq query

I have a linq query in which I want to include those entries that are not null or empty in the database field, but when I use string.isNullorEmpty this gives me an error. How can I achieve this task, my request

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 allows me to SQL Server not handle NText, Text, Xml or Image type comparison

+3
source share
5 answers

, - , , nvarchar text/ntext.

EDIT: , ; , LINQ to SQL . DBML .

+5

:

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

You tried to replace

where string.IsNullOrEmpty(pstmt.vcr_MetaValue)

with

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

?

+1
source

If the database field is NTEXT, you can check to see if the field is empty. Here is 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
+1
source

I'm not sure which linq provider you are using, and from quick google-ing it seems that IsNullOrEmpty is not universally supported. Answers that occurred when entering text look correct.

0
source

All Articles