Orderby with ?? operator failed

Posts   
 
    
savanna
User
Posts: 21
Joined: 20-Mar-2008
# Posted on: 13-May-2008 02:26:05   

Hi, Otis

I run into another issue today with ?? operator in C#. I want to order by the result of coalescing 2 columns. The query that I have is similar to this:

    LinqMetaData mdata = new LinqMetaData(adapter);
    var results = from o in mdata.Employee
                  orderby o.BirthDate ?? o.HireDate
                  select o; 

This query works on NorthWind but may not be practically meaningful, only to demonstrate this problem.

The SQL query it generated is:

SELECT [LPLA_1].[EmployeeID] AS [EmployeeId], [LPLA_1].[LastName], [LPLA_1].[FirstName], [LPLA_1].[Title], [LPLA_1].[TitleOfCourtesy], [LPLA_1].[BirthDate], [LPLA_1].[HireDate], [LPLA_1].[Address], [LPLA_1].[City], [LPLA_1].[Region], [LPLA_1].[PostalCode], [LPLA_1].[Country], [LPLA_1].[HomePhone], [LPLA_1].[Extension], [LPLA_1].[Photo], [LPLA_1].[Notes], [LPLA_1].[ReportsTo], [LPLA_1].[PhotoPath] FROM [Northwind].[dbo].[Employees] [LPLA_1] ORDER BY [LPFA_1] ASC

It seems the ORDER BY failed to be generated. It's empty.

I also tried this using LinqToSql. It works as expected. I am wondering do I have to create a function mapping or what not.

Thanks in advance!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 13-May-2008 09:20:28   

Will look into it simple_smile

(edit) bug in runtime. The thing is that when a field in a sortclause has an expression, the sortexpression.ToQueryText() assumes that the field is also present as-is in the selectlist, and therefore refers to it as the alias, as in some databases that's the only way it works. It should receive the list of fields in the selectlist so it can check whether a sortclause should be emitted with full target/expression or with the alias.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 13-May-2008 18:49:00   

Fixed it.


LinqMetaData metaData = new LinqMetaData(adapter);
var q = from o in metaData.Order
        orderby o.ShippedDate ?? o.OrderDate
        select o;

int count = 0;
DateTime? prev = null;
DateTime? curr = null;
bool firstTime = true;
foreach(var v in q)
{
    prev = curr;
    curr = v.ShippedDate ?? v.OrderDate;
    if(!firstTime)
    {
        Assert.IsTrue(prev <= curr);
    }
    count++;
    firstTime = false;
}

Assert.AreEqual(818, count);

I'll attach new builds of runtime and linqsupportclasses. You need both to get it working.

(see attached files)

Frans Bouma | Lead developer LLBLGen Pro
savanna
User
Posts: 21
Joined: 20-Mar-2008
# Posted on: 13-May-2008 19:11:43   

Thank you Otis! I will check it out now.