简体   繁体   中英

System.NotSupportedException: How to simplify the NHibernate Query?

How to simplify the below query in NHibernate?

Below is the logic to find the Max value between 3 different products and order them by the Max value.

IQueryable<Property> results = ISession.Get<Property>();

results =   from r in results
            let pdts = new List<decimal?> { r.Prod1.Rate, r.Prod2.Rate, r.Prod3.Rate }
            let max = pdts.Max()
            orderby max
            select r;

When executing it, NHibernate is throwing an error System.NotSupportedException with the exceptionMessage

"exceptionMessage": "new List 1() {Void Add(System.Nullable 1[System.Decimal])([100001].Prod1.Rate), Void Add(System.Nullable 1[System.Decimal])([100001].Prod2.Rate), Void Add(System.Nullable 1[System.Decimal])([100001].Prod3.Rate)}", "exceptionType": "System.NotSupportedException",

How can I simplify this query as the logic is perfect?

The reason is that NHibernate cannot generate the SQL from your Linq because of the List initialization is.

Try this:

results = from r in results
          let max = (r.Prod1.Rate >= r.Prod2.Rate && r.Prod1.Rate >= r.Prod3.Rate) ? r.Prod1.Rate
                  : (r.Prod2.Rate >= r.Prod1.Rate && r.Prod2.Rate >= r.Prod3.Rate) ? r.Prod2.Rate
                  : r.Prod3.Rate
          orderby max
          select r;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM