Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ syntax where string value is not null or empty

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

like image 875
Jon Erickson Avatar asked Sep 02 '09 16:09

Jon Erickson


People also ask

How do you check if a string is NULL or empty?

Using the isEmpty() Method The isEmpty() method returns true or false depending on whether or not our string contains any text. It's easily chainable with a string == null check, and can even differentiate between blank and empty strings: String string = "Hello there"; if (string == null || string.

IS NOT NULL check in LINQ?

The query needs to look for the values that is not null in any one of the list values (100 or 110 or 120). model = (from line in db. Bibs where line. TNo == "245" && (line.

How do you handle NULL values in LINQ query?

An object collection such as an IEnumerable<T> can contain elements whose value is null. If a source collection is null or contains an element whose value is null , and your query doesn't handle null values, a NullReferenceException will be thrown when you execute the query. var query1 = from c in categories where c !=


2 Answers

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=367077

Problem Statement
It's possible to write LINQ to SQL that gets all rows that have either null or an empty string in a given field, but it's not possible to use string.IsNullOrEmpty to do it, even though many other string methods map to LINQ to SQL. Proposed Solution Allow string.IsNullOrEmpty in a LINQ to SQL where clause so that these two queries have the same result:

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

Other Reading:
1. DevArt
2. Dervalp.com
3. StackOverflow Post

like image 133
RSolberg Avatar answered Sep 27 '22 21:09

RSolberg


This won't fail on Linq2Objects, but it will fail for Linq2SQL, so I am assuming that you are talking about the SQL provider or something similar.

The reason has to do with the way that the SQL provider handles your lambda expression. It doesn't take it as a function Func<P,T>, but an expression Expression<Func<P,T>>. It takes that expression tree and translates it so an actual SQL statement, which it sends off to the server.

The translator knows how to handle basic operators, but it doesn't know how to handle methods on objects. It doesn't know that IsNullOrEmpty(x) translates to return x == null || x == string.empty. That has to be done explicitly for the translation to SQL to take place.

like image 39
Brian Genisio Avatar answered Sep 27 '22 22:09

Brian Genisio