12

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.

Alconja
  • 14,696
  • 3
  • 59
  • 61
rudnev
  • 2,241
  • 3
  • 21
  • 31

4 Answers4

12

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.

Benjamin Autin
  • 4,083
  • 24
  • 33
-1

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
  • 47,131
  • 14
  • 97
  • 117
-1

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
  • 22,531
  • 24
  • 88
  • 119
-1

There is a workaround on The || (or) Operator in Linq with C# according to which you would do in your case something like:

.Where(t =>  t.SomeProperty >= Convert.ToDecimal(someNullstring ?? "0"))

Of course this might not be the solution you need in your particular case, but at least it gives an idea how to bypass the error.

Community
  • 1
  • 1
yoel halb
  • 11,417
  • 3
  • 53
  • 50