LINQ syntax where string value is not null or empty

Viewed 140604

I'm trying to do a query like so...

query.Where(x => !string.IsNullOrEmpty(x.PropertyName));

but it fails...

so for now I have implemented the following, which works...

query.Where(x => (x.PropertyName ?? string.Empty) != string.Empty);

is there a better (more native?) way that LINQ handles this?

EDIT

apologize! didn't include the provider... This is using LINQ to SQL

4 Answers

... 12 years ago :) But still, some one may found it helpful:

Often it is good to check white spaces too

query.Where(x => !string.IsNullOrWhiteSpace(x.PropertyName));

it will converted to sql as:

WHERE [x].[PropertyName] IS NOT NULL AND ((LTRIM(RTRIM([x].[PropertyName])) <> N'') OR [x].[PropertyName] IS NULL)

or other way:

query.Where(x => string.Compare(x.PropertyName," ") > 0);

will be converted to sql as:

WHERE [x].[PropertyName] > N' '
Related