igor-tkachev / bltoolkit

Business Logic Toolkit for .NET
MIT License
297 stars 113 forks source link

Dependence on the order of the arguments provided a comparison with null #317

Closed spencode closed 9 years ago

spencode commented 10 years ago

1) I downloaded "Current repository snapshot (v.4.1)" from http://bltoolkit.net/Download.ashx

2) Run my code (part code):

from re2 in something2.DefaultIfEmpty()
select new
{
  PhoneNumber = de.EntityValue,
  FieldName = fld.Name,
  dev.DisplayString,
  Id = de.DictionaryEntityId,
  de.DictionaryId,
  LineState = (null == re2) ? TerminalLineState.Unavailable : re2.LineState
}).ToArray();

and has got error:

System.InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.FullConditionalExpression' to type 'System.Linq.Expressions.ConstantExpression'.
   at BLToolkit.Data.Linq.Builder.ExpressionBuilder.<>c__DisplayClassd4.<CanBeTranslatedToSql>b__d3(Expression pi) in d:\Projects\dev\src\Dependencies\vendor\BLToolkit\Source\Data\Linq\Builder\ExpressionBuilder.SqlBuilder.cs:line 2203
   at BLToolkit.Linq.ExpressionHelper.Find(Expression expr, Func`2 func) in d:\Projects\dev\src\Dependencies\vendor\BLToolkit\Source\Linq\ExpressionHelper.cs:line 1087
   at BLToolkit.Data.Linq.Builder.ExpressionBuilder.CanBeTranslatedToSql(IBuildContext context, Expression expr, Boolean canBeCompiled) in d:\Projects\dev\src\Dependencies\vendor\BLToolkit\Source\Data\Linq\Builder\ExpressionBuilder.SqlBuilder.cs:line 2201
   at BLToolkit.Data.Linq.Builder.ExpressionBuilder.<>c__DisplayClasse1.<BuildExpression>b__e0(Expression pi) in d:\Projects\dev\src\Dependencies\vendor\BLToolkit\Source\Data\Linq\Builder\ExpressionBuilder.QueryBuilder.cs:line 97
   at BLToolkit.Linq.ExpressionHelper.Convert2(Expression expr, Func`2 func) in d:\Projects\dev\src\Dependencies\vendor\BLToolkit\Source\Linq\ExpressionHelper.cs:line 1729
   at BLToolkit.Linq.ExpressionHelper.Convert2[T](IEnumerable`1 source, Func`2 func) in d:\Projects\dev\src\Dependencies\vendor\BLToolkit\Source\Linq\ExpressionHelper.cs:line 1631
   at BLToolkit.Linq.ExpressionHelper.Convert2(Expression expr, Func`2 func) in d:\Projects\dev\src\Dependencies\vendor\BLToolkit\Source\Linq\ExpressionHelper.cs:line 1867
   at BLToolkit.Data.Linq.Builder.ExpressionBuilder.BuildExpression(IBuildContext context, Expression expression) in d:\Projects\dev\src\Dependencies\vendor\BLToolkit\Source\Data\Linq\Builder\ExpressionBuilder.QueryBuilder.cs:line 22
   at BLToolkit.Data.Linq.Builder.SelectContext.BuildExpression(Expression expression, Int32 level) in d:\Projects\dev\src\Dependencies\vendor\BLToolkit\Source\Data\Linq\Builder\SelectContext.cs:line 89
   at BLToolkit.Data.Linq.Builder.SelectContext.BuildQuery[T](Query`1 query, ParameterExpression queryParameter) in d:\Projects\dev\src\Dependencies\vendor\BLToolkit\Source\Data\Linq\Builder\SelectContext.cs:line 64
   at BLToolkit.Data.Linq.Builder.ExpressionBuilder.Build[T]() in d:\Projects\dev\src\Dependencies\vendor\BLToolkit\Source\Data\Linq\Builder\ExpressionBuilder.cs:line 150
   at BLToolkit.Data.Linq.Query`1.GetQuery(IDataContextInfo dataContextInfo, Expression expr) in d:\Projects\dev\src\Dependencies\vendor\BLToolkit\Source\Data\Linq\Query.cs:line 140
   at BLToolkit.Data.Linq.ExpressionQuery`1.GetQuery(Expression expression, Boolean cache) in d:\Projects\dev\src\Dependencies\vendor\BLToolkit\Source\Data\Linq\ExpressionQuery.cs:line 77
   at BLToolkit.Data.Linq.ExpressionQuery`1.Execute(IDataContextInfo dataContextInfo, Expression expression) in d:\Projects\dev\src\Dependencies\vendor\BLToolkit\Source\Data\Linq\ExpressionQuery.cs:line 69
   at BLToolkit.Data.Linq.ExpressionQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() in d:\Projects\dev\src\Dependencies\vendor\BLToolkit\Source\Data\Linq\ExpressionQuery.cs:line 149
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)

If we write the condition as (changed the procedure comparison with null):

LineState = (re2 == null) ? TerminalLineState.Unavailable : re2.LineState

it everything works fine

p.s. In version 4.0, it worked fine regardless of the order of arguments comparison with null.