12745

Nhibernate Linq Sum with calculation causes NotSupportedException

Question:

I am using Nhibernate 3.3.3.4001 with SybaseSQLAnywhere12Dialect and trying to use a simple calculation within a sum linq function.

I would like to use Linq rather than HQL to achieve this and would be happy to extend Nhibernate with a default linq to hql generator.

Using the following Linq query raises the exception

<blockquote>

System.NotSupportedException: Expression type 'NhSumExpression' is not supported by this SelectClauseVisitor

</blockquote>

<strong>Linq Query</strong>

var query = (from c in session.Query<Entity>() select c.FieldValue / 100M).Sum()

<strong>Expected SQL Statement</strong>

SELECT SUM(FieldValue / 100.0) FROM TableName

If the same query is run without the Sum() function it performs the calculation correctly, as such am confused why the Sum() wouldn't work.

Answer1:

You may try to rewrite your LINQ query a bit, just like this one:

var result = ((from c in session.Query<Entity>() select c.FieldValue).Sum()) / 100;

Resulting query could also be optimized by moving division operator out of the sum:

SELECT SUM(FieldValue) / 100 FROM TableName

Answer2:

There is an open issue when using constants within Linq group function (Max, Min, Sum) functions. If another field is used it will work correctly.

Found bug within NHibernate 3.3.3<br /><a href="https://nhibernate.jira.com/browse/NH-3376" rel="nofollow">https://nhibernate.jira.com/browse/NH-3376</a>

Recommend

  • Simple C function to MIPS instructions
  • not able to create VC++ project, with VS11
  • C++ and JS RegEx equivalent of \\p{L}
  • Return null in boolean to checkbox state converter in XAML
  • Azure table query partial partitionkey guid match
  • Tools for understanding HTML layout
  • SIP API media codecs
  • Connecting bluetooth device to windows phone 8 application
  • Issue with routerLink directive
  • Create a link to a web page that runs a Javascript function on the page
  • PDO error when wrong host name
  • powershell Get-Counter -ComputerName parameter on Windows 7
  • Xamarin MonoAndroid Azure mobile service InsertAsync
  • Suqueries in select clause with JPA
  • Exception creating JSON with LINQ
  • Does Apportable support to build library binary (.a/.so)?
  • Can I read an iPhone beacon with Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData
  • TFS 2015 - Waiting for an agent to be requested
  • NUnit 3.0 TestCase const custom object arguments
  • Plotting line graph with factors in R
  • URLConnection doesn't work since API 10 and higher?
  • 550 Access denied - Invalid HELO name
  • Problems installing Yesod for Haskell
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Converting a WriteableBitmap image ToArray in UWP
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • FFmpeg Conversion Error
  • Lost migrations and Azure database is now out of sync
  • Why value captured by reference in lambda is broken? [duplicate]
  • Launch Runnable Jar from Web Start
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • javascript inside java/jsp code
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Android Studio and gradle
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Binding checkboxes to object values in AngularJs
  • How can i traverse a binary tree from right to left in java?