WilcoB wrote:
I use selfservicing, and I'm trying to fetch entities using an entity collection. I want to sort the entities by a field called 'Weight', which appears in 2 tables. The generated query is valid in SQL server, but the LLBLGen runtime doesn't seem to allow this.
Sorry to nittpick, but it's not clear what the error is exactly. What's the exact exception, stacktrace and what's the code you're executing? The reason I ask this is because there is no check code in the runtime library which checks if a field is added twice to a sortexpression. It doesn't care.
I'll try to reproduce it by fetching customer entities and sort on customer.customerid AND order.customerID, however I'm not sure if I'm testing what you're running, so it would be great if you paste the code you're executing, the exception message and the stacktrace
Edit: Ok I reproduced it with:
[Test]
public void ComplexSortCollectionFetchTest()
{
CustomerCollection customers = new CustomerCollection();
IRelationCollection relations = new RelationCollection();
ISortExpression sorter = new SortExpression();
relations.Add(CustomerEntity.Relations.OrderEntityUsingCustomerId);
sorter.Add(SortClauseFactory.Create(CustomerFieldIndex.CustomerId, SortOperator.Ascending));
sorter.Add(SortClauseFactory.Create(OrderFieldIndex.CustomerId, SortOperator.Ascending));
customers.GetMulti(null, 0, sorter, relations);
}
Looking into it.
(edit)
Hmm. The query, as you said, is not wrongly generated:
SELECT [dbo].[Customers].[CustomerID] AS [CustomerId],
[dbo].[Customers].[CompanyName] AS [CompanyName],
[dbo].[Customers].[ContactName] AS [ContactName],
[dbo].[Customers].[ContactTitle] AS [ContactTitle],
[dbo].[Customers].[Address] AS [Address],
[dbo].[Customers].[City] AS [City],
[dbo].[Customers].[Region] AS [Region],
[dbo].[Customers].[PostalCode] AS [PostalCode],
[dbo].[Customers].[Country] AS [Country],
[dbo].[Customers].[Phone] AS [Phone],
[dbo].[Customers].[Fax] AS [Fax]
FROM (
[dbo].[Customers] INNER JOIN [dbo].[Orders] ON
[dbo].[Customers].[CustomerID]=[dbo].[Orders].[CustomerID]
)
ORDER BY
[dbo].[Customers].[CustomerID] ASC,
[dbo].[Orders].[CustomerID] ASC
But SqlServer apparently can't distinguish the fields in the Order By clause, even though the full schema + table name is specified.
What's even more weird is the fact that if I specify an alias for Orders and customers, it works, IF I also select from the alias (thus do C.* instead of customers.*)
(edit)
It apparently works if the select is from the aliased table + everything else is aliased, OR if I change the alias for the CustomerID field in the select list, as the order by apparently sees 2 columns in the join list with the same name which don't look the same suddenly when the tables are aliased... Clearly a bug in sqlserver (as the full column specification is specified in the order by clause so it can determine which fields to sort on), but that doesn't help much.
The problem is: the GetMulti() call chain creates its own fieldlist for the select query producing routine. This means that you can specify aliasses for the tables in the relation collection, for the sortfields but not for the fields in the select list, which makes the query fail.
The only workaround for now is to rename the Weight field in one of the entities, so the select list will have a different alias, so the SqlServer query processor doesn't get confused