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,834
  • 3
  • 60
  • 61
rudnev
  • 2,281
  • 3
  • 22
  • 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,143
  • 26
  • 34
-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,767
  • 15
  • 98
  • 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
  • 23,005
  • 25
  • 91
  • 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
  • 12,188
  • 3
  • 57
  • 52