3

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. 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

1
  • Boolean IsNullOrEmpty(System.String)' has no supported translation to SQL Commented May 4, 2011 at 10:03

5 Answers 5

5

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.

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. You need to keep your DBML in sync with your actual database schema.

Sign up to request clarification or add additional context in comments.

2 Comments

@Fraz: Are you absolutely sure? The error message strongly suggests it's not. Was it text / ntext at some earlier time? Perhaps you need to update the LINQ to SQL entities...
Yes my mistake i forgot to update the DBML after changing the data type in database
2

Have you tried replacing

where string.IsNullOrEmpty(pstmt.vcr_MetaValue)

with

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

?

1 Comment

it give me SQL Server does not handle comparison of NText, Text, Xml, or Image data types error
1

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

Comments

1

If DB field is NTEXT, You can check if field is not like empty string. 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

1 Comment

Like method does not check null fields, so I've removed null check from example.
0

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. The answers that popped up while typing this look correct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.