views:

256

answers:

4

Why linq is trying to check second expression anyway?

.Where(t =>  String.IsNullOrEmpty(someNullString) || t.SomeProperty >= Convert.ToDecimal(someNullstring))

What is usual workaround?

Update:
It is about LINQ to SQL, of course. It cannot translate to SQL.

+1  A: 

Does this help?

.Where(t =>  String.IsNullOrEmpty(someNullString) || (t.SomeProperty >= Convert.ToDecimal(someNullstring)))

Noticed the () around the second condition? I don't think it works but in general I prefer to put () around every condition in my code. That way, the compiler knows which parts belong together when it compiles the code, to prepare it for short-circuit evaluation...

Workshop Alex
+1  A: 

I can't reproduce any problem with the short circuit evaluation...

I think this evaluates to something like:

[CompilerGenerated]
private static bool <MyMethod>b__f(MyObject t)
{
    return (String.IsNullOrEmpty(someNullString) 
                 || t.SomeProperty >= Convert.ToDecimal(someNullstring));
}

short circuit works well here.

I suspect other elements in your Enumerable evaluate the first condition (String.IsNullOrEmpty(someNullString)) to false. Can you confirm this?

Provide a bit more code so that we can analyze this.

bruno conde
+6  A: 

Is the .Where being used on a Table<>?

If so, then before any data can be grabbed, it must convert the LINQ to SQL and to do that it must convert the string into a decimal. It's not trying to actually perform the comparisons yet, it's trying to build the constructs necessary to retrieve data.

toast
+1  A: 

Do you have a variable t in any scope that may be evaluated?

Did you try with parenthesis like this:

.Where(t =>  (String.IsNullOrEmpty(someNullString) || 
             t.SomeProperty >= Convert.ToDecimal(someNullstring)))

?

eKek0
The => defines the scope of t.
toast