Home Dashboard Directory Help
Search

I want to use string.IsNullOrEmpty in LINQ to SQL statements. by Kyralessa


Status: 

Closed
 as External Help for as External


11
0
Sign in
to vote
Type: Suggestion
ID: 367077
Opened: 9/10/2008 1:44:46 PM
Access Restriction: Public
0
Workaround(s)
view

Description

I can easily create a LINQ to SQL query that gets rows where a field is null like this:

var fieldNull =
    from item in db.SomeTable
    where item.SomeField == null
    select item;


I can get rows that contain an empty string like this:

var fieldEmpty =
    from item in db.SomeTable
    where item.SomeField.Equals(string.Empty)
    select item;


I can get both like this:

var fieldNullOrEmpty =
    from item in db.SomeTable
    where item.SomeField == null || item.SomeField.Equals(string.Empty)
    select item;


But I *can't* get both like this:

var fieldNullOrEmpty =
    from item in db.SomeTable
    where string.IsNullOrEmpty(item.SomeField)
    select item;


Why not? Many of the other string methods are honored in LINQ to SQL, so why not this one? It would be easy enough to translate it it to the same thing as the previous query, to retrieve rows that either have an empty string or are null.
Details
Sign in to post a comment.
Posted by S_Shevyrov on 6/28/2012 at 11:55 PM
Apparently the string.IsNullOrEmpty() method is not working with the 'text' data type and is working with 'varchar(MAX)'.

'ntext' , 'text', and 'image' data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead. Source: http://msdn.microsoft.com/en-us/library/ms187993.aspx
Posted by S_Shevyrov on 6/26/2012 at 5:14 PM
It is the middle of 2012, Entity Framework version 4.3.1 and the issue still has not been resolved by Microsoft since they promised to do this in 2008. So much for the promises.
Posted by kainhart on 10/20/2010 at 3:57 PM
akukula, thanks for the update. It's a shame these are not supported in Linq to SQL but I guess thats just more evidence that Microsoft is truely trying to slowly get rid of L2S in favor of Entity Framework.

have you tried this in .NET 4.0 RTM?
Posted by aqx on 2/10/2010 at 4:54 PM
I just tried it on Visual Studio 2010 RC1 (target framework 4.0):
Method 'Boolean IsNullOrEmpty(System.String)' has no supported translation to SQL.
Method 'Boolean IsNullOrWhiteSpace(System.String)' has no supported translation to SQL.

However -- it runs with no problem with Entity Framework classes.
Posted by aqx on 2/6/2010 at 3:57 AM
For all of you that wonder what happened:

I just tried it on Visual Studio 2010 beta2 (target framework 4.0):
Method 'Boolean IsNullOrEmpty(System.String)' has no supported translation to SQL.
Method 'Boolean IsNullOrWhiteSpace(System.String)' has no supported translation to SQL.

Posted by kainhart on 12/15/2009 at 4:41 AM
This comment is closed as external, does that mean that this issue has been taken up for consideration in .NET 4.0? If so it would be nice to provide a link to any relevant documentation or to the corresponding .Connect issue.
Posted by Rich Collette on 10/30/2008 at 8:59 AM
I suggest that this be included in Linq to Entities as well.
Posted by Microsoft on 9/26/2008 at 3:56 PM
Hi,

Thanks for your suggestion. We will consider this for a future version of LINQ to SQL.

Thanks,
LINQ to SQL Team
Sign in to post a workaround.