Search code examples
c#nhibernatenhibernate-mappingqueryoverlinq-to-nhibernate

could not resolve property: CS$<>8__locals2 of: BusinessObjectMain; Nhibernate QueryOver Linq, lambda expression inside a for loop


We use Vs2019 with an older version of Nhibernate 3.2. We recently upgraded to VS2019. This code worked correctly in VS2010.
We are getting this error with Nibernate QueryOver, lambda expressions inside a where clause. I stripped it down to a minimal version. During runtime Block 1 fails and Block 2 does not. Why? I can't figure this out. Could it be something related to .net framework 4.8, roselyn or an older version of Nhibernate? I didn't post all the Business Objects here but if needed I can do so. I am also seeing this behavior across the application for different Business Objects.

could not resolve property: CS$<>8__locals2 of: BusinessObjectMain

Block 1

var EventQuery = session.QueryOver<BusinessObjectMain()
                    .JoinAlias(ce => ce.Bo1, () => Bo1)
                    .JoinAlias(ce => ce.Bo2, () => Bo2, JoinType.LeftOuterJoin)
                    .JoinAlias(() => Bo2.Bo3, () =>Bo3, JoinType.LeftOuterJoin)
                    ;



for (int i = 0; i < 1; i++) {

  object[] arr = new object[1];
  arr[0] = 202;
  EventQuery = EventQuery.Where(() => Bo3.TypeId.IsIn(arr));
}


var Results = EventQuery.Future<Data>();
 var list = Results.ToList();---Error happens here.  could not resolve property: CS$<>8__locals2 of: BusinessObjectMain

Why does the code below work? If I move the Where clause outside the for loop, it works correctly without any errors

Block 2

 var EventQuery = session.QueryOver<BusinessObjectMain()
                        .JoinAlias(ce => ce.Bo1, () => Bo1)
                        .JoinAlias(ce => ce.Bo2, () => Bo2, JoinType.LeftOuterJoin)
                        .JoinAlias(() => Bo2.Bo3, () =>Bo3, JoinType.LeftOuterJoin)
                        ;
    
      object[] arr = new object[1];
      arr[0] = 202;
      EventQuery = EventQuery.Where(() => Bo3.TypeId.IsIn(arr));
    
    for (int i = 0; i < 1; i++) {
    
     
    }
    
    
    var Results = EventQuery.Future<Data>();
     var list = Results.ToList()

Solution

  • It's a bug in NHibernate related to Roslyn compiler that's used since Visual Studio 2015 (because Roslyn compiles lambda's differently). See https://nhibernate.jira.com/browse/NH-3795, https://github.com/nhibernate/nhibernate-core/pull/441

    It's fixed in NHibernate versions 3.3.5, 3.4.1 and 4.0.4+