PixelPaul
PixelPaul

Reputation: 2767

LINQ .equals() and string variables

Not sure why this query returns no results (note that CUSTOMERs is from an Oracle database):

string zip = "12345"
string state = "MA"

var result = from x in CUSTOMERs
             where x.STATE.Equals(state) || x.ZIP.Equals(zip)
             select x;

But this one does?

var result = from x in CUSTOMERs
             where x.STATE.Equals("MA") || x.ZIP.Equals("12345")
             select x;

Upvotes: 0

Views: 181

Answers (1)

Mrinal Kamboj
Mrinal Kamboj

Reputation: 11482

Following is my understanding, you are using a Oracle data connector in the LinqPad, which allows you to Query a Customers Table, now since you are running an IQueryable<T> Linq Query, which unlike Linq to Objects, execute at the remote data source and takes an Expression tree in the form Expression<Func<T,bool>. It is not able to bind the values when supply a variable instead of actual hard coded value. Issue with binding values is purely due to the way it expects and parse the Expression tree internally.

To verify my point, check the signature of CUSTOMERs.Where in the Fluent syntax and it would be taking an expression tree instead of Func<T,bool> required by the Linq2Objects

Upvotes: 1

Related Questions