Bug #77735 OrderBy Case CaseFragment.Accept(SqlFragmentVisitor visitor)NullReferenceExcepti
Submitted: 15 Jul 2015 17:14 Modified: 21 Aug 2015 6:11
Reporter: fu xuejun Email Updates:
Status: No Feedback Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.9.6 OS:Windows (.net 4.5)
Assigned to: Assigned Account CPU Architecture:Any

[15 Jul 2015 17:14] fu xuejun
Description:
I use like this:
    
       return
                RoomRepository.Entities.Where(
                    room => room.Code == code && (!String.IsNullOrEmpty(status) ? room.STATUS == status : true))
//                          ;
             // if i use order by function it will raise NullReferenceException
             .OrderBy(room => room.ROOM_NO);

when I use OrderBy fuction:

行 282:    internal override void Accept(SqlFragmentVisitor visitor)
行 283:    {
行 284:      Else.Accept(visitor);
行 285:      for (int i = 0; i < Then.Count; i++)
行 286:        Then[i].Accept(visitor);

[NullReferenceException: 未将对象引用设置到对象的实例。]
   MySql.Data.Entity.CaseFragment.Accept(SqlFragmentVisitor visitor) in D:\work\zkar\git\dataservice\MySql.Data.Entity\Fragments\SqlFragment.cs:284
   MySql.Data.Entity.CaseFragment.Accept(SqlFragmentVisitor visitor) in D:\work\zkar\git\dataservice\MySql.Data.Entity\Fragments\SqlFragment.cs:286
   MySql.Data.Entity.BinaryFragment.Accept(SqlFragmentVisitor visitor) in D:\work\zkar\git\dataservice\MySql.Data.Entity\Fragments\SqlFragment.cs:215
   MySql.Data.Entity.BinaryFragment.Accept(SqlFragmentVisitor visitor) in D:\work\zkar\git\dataservice\MySql.Data.Entity\Fragments\SqlFragment.cs:216
   MySql.Data.Entity.SqlGenerator.VisitAndReplaceTableName(SqlFragment sf, String oldTable, String newTable, Dictionary`2 dicColumns) in D:\work\zkar\git\dataservice\MySql.Data.Entity\Generators\SqlGenerator.cs:600
   MySql.Data.Entity.SqlGenerator.FuseSelectWithInnerSelect(SelectStatement outer, SelectStatement inner) in D:\work\zkar\git\dataservice\MySql.Data.Entity\Generators\SqlGenerator.cs:536
   MySql.Data.Entity.SqlGenerator.TryFusingSelect(InputFragment f) in D:\work\zkar\git\dataservice\MySql.Data.Entity\Generators\SqlGenerator.cs:510
   MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type) in D:\work\zkar\git\dataservice\MySql.Data.Entity\Generators\SqlGenerator.cs:612
   MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type) in D:\work\zkar\git\dataservice\MySql.Data.Entity\Generators\SelectGenerator.cs:442
   MySql.Data.Entity.SelectGenerator.Visit(DbProjectExpression expression) in D:\work\zkar\git\dataservice\MySql.Data.Entity\Generators\SelectGenerator.cs:428
   System.Data.Common.CommandTrees.DbProjectExpression.Accept(DbExpressionVisitor`1 visitor) +20
   MySql.Data.Entity.SelectGenerator.GenerateSQL(DbCommandTree tree) in D:\work\zkar\git\dataservice\MySql.Data.Entity\Generators\SelectGenerator.cs:63
   MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree) in D:\work\zkar\git\dataservice\MySql.Data.Entity\ProviderServices.cs:124
   System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree) +111
   System.Data.EntityClient.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree) +1257
   System.Data.EntityClient.EntityProviderServices.CreateCommandDefinition(DbProviderFactory storeProviderFactory, DbCommandTree commandTree) +49
   System.Data.EntityClient.EntityProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree) +115
   System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree) +111
   System.Data.Objects.Internal.ObjectQueryExecutionPlan.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Span span, ReadOnlyCollection`1 compiledQueryParameters, AliasGenerator aliasGenerator) +212
   System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) +1106
   System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +197
   System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +37
   System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator() +59
   System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +40
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +390
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +54

How to repeat:
                EntitySet<Room>.Where(
                    room => room.Code == code && (!String.IsNullOrEmpty(status) ? room.STATUS == status : true))
//                          ;
             // if i use order by function it will raise NullReferenceException
             .OrderBy(room => room.ROOM_NO);
[15 Jul 2015 17:16] fu xuejun
I use EF5 with asp.net MVC5 and the framework version is 4.5.
[15 Jul 2015 17:22] fu xuejun
I debug the source code with the connector, the I find

{(`Extent1`.`Code` = @p__linq__0) AND 
((CASE WHEN 
(NOT ((@p__linq__1 IS  NULL) OR ((LENGTH(@p__linq__1)) = 0)))
 THEN 
 (CASE 
 WHEN 
 (`Extent1`.`Status` = @p__linq__2) 
 THEN (1)  
 WHEN (`Extent1`.`Status` != @p__linq__2) 
 THEN (0) END)
   
 ELSE (1) 
 END) = 1)}

I find there no else in case when then else statement.
So I change My code like below:

                RoomRepository.Entities.Where(
                    room => room.Code == code && (!String.IsNullOrEmpty(status) && room.STATUS == status ? true : false))
//                          ;
             .OrderBy(room => room.ROOM_NO);

It works fine.

But I want my old code works well, too.
can you fix that?

My English is so bad, sorry.
[16 Jul 2015 6:06] Chiranjeevi Battula
Hello fu xuejun,

Thank you for the bug report.
Could you please provide repeatable test case (exact create table statements, model, context class, etc.) to confirm this issue at our end?

Thanks,
Chiranjeevi.
[16 Jul 2015 7:34] fu xuejun
I also find, when the Status Field is null in database , it will not raise exception, but there is no data return.
[21 Jul 2015 6:11] Chiranjeevi Battula
Hello fu xuejun,

Thank you for your feedback.
I could not repeat the issue using with EntityFramework 6.1.3.
Could you please try with EF6.1.3 and let us know if you are still facing issue with repeatable steps.

Thanks,
Chiranjeevi.
[22 Aug 2015 1:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".