Linq to SQL Max function on integer data type results in computation being done on the client

Posts   
 
    
acl
User
Posts: 94
Joined: 28-Mar-2012
# Posted on: 29-Apr-2024 14:41:42   

Hi,

we are currently using LLBLGen Pro Runtime Framework Version 5.10.2 (using the adapter style) and planning on upgrading to the latest version soon.

I observed something very strange the other day.

Take the following Linq query for instance:

Dim maxId = new LinqMetaData(adapter).MyTable.Max(Function(x) x.MyPrimaryKeyColumn)

This generates a query that loads the entire table "MyTable" (all columns). The MyPrimaryKeyColumn column is of data type integer.

I managed to reproduce this with lots of tables. However, I was not able to reproduce it with string and date columns.

I probably did not notice this before since I used to do the following, and only more recently started to drop the "select":

' This works
Dim maxId = new LinqMetaData(adapter).MyTable.Select(Function(x) x.MyPrimaryKeyColumn).Max()

Is this a bug? A pecularity of expression trees in vb.net?

Thanks,

andreas

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39625
Joined: 17-Aug-2003
# Posted on: 30-Apr-2024 07:47:35   

Welcome to the wonderful world of .NET simple_smile

In C#, the 'Select' is often not needed, and they produce an expression tree which shows intent. In VB.NET this isn't the case, where the .Select() is mandatory to make sure what is meant with the query.

So indeed, the VB.NET query requires a .Select() call to get an expression tree that can be transformed to a SQL query, as the C# quirk of not always requiring the .Select() call results in a check if we're dealing with VB.NET or C# when examining the expression tree: if there's no Select() we need to silently add it, but if it's VB.NET we don't as it's somewhere in the tree.

Frans Bouma | Lead developer LLBLGen Pro
acl
User
Posts: 94
Joined: 28-Mar-2012
# Posted on: 30-Apr-2024 10:37:19   

Thanks for your fast reply. Ok, I'll note that I must not use the "short" syntax in VB.NET.

acl
User
Posts: 94
Joined: 28-Mar-2012
# Posted on: 16-May-2024 00:00:25   

Hi Frans,

just a small note. You might want to change the documentation to reflect this.

The following page uses the syntax that should not be used as of this thread:

https://www.llblgen.com/Documentation/5.1/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/Linq/gencode_linq_generalusage.htm#dont-use-the-aggregate-keyword

It says to use

Dim max = metaData.Order.Max(Function(o) o.OrderDate)

Which is not the correct way, as we've seen here.

Best,

andreas

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39625
Joined: 17-Aug-2003
# Posted on: 16-May-2024 08:43:26   

acl wrote:

Hi Frans,

just a small note. You might want to change the documentation to reflect this.

The following page uses the syntax that should not be used as of this thread:

https://www.llblgen.com/Documentation/5.1/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/Linq/gencode_linq_generalusage.htm#dont-use-the-aggregate-keyword

It says to use

Dim max = metaData.Order.Max(Function(o) o.OrderDate)

Which is not the correct way, as we've seen here.

Best,

andreas

Thanks. We've fixed v5.10/5.11 docs with this issue. simple_smile

Frans Bouma | Lead developer LLBLGen Pro