Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Newbie to Linq
Message
De
12/08/2014 05:58:45
 
 
À
11/08/2014 14:05:59
Information générale
Forum:
ASP.NET
Catégorie:
LINQ
Titre:
Versions des environnements
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01605340
Message ID:
01605483
Vues:
41
>>>>>That did it guys ( ordered it in the invoice loop )
>>>>
>>>>Pity (g) - I would love to have known why the OrderBy/GroupBy option didn't work.
>>>>Maybe Rob knows ?
>>>>
>>>> very strange though
>>>
>>>Not entirely sure, but if I had to guess, the grouping operation might be done in parallel, so the order may be lost when the results are recombined.
>>
>>I thought Linq was sequential (unless you specifically use PLinq) but I could be wrong.
>>
>>I came across this (which, of course, may not be correct): http://stackoverflow.com/questions/204505/preserving-order-with-linq
>>
>>Snip : " GroupBy - The IGrouping objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping. Elements in a grouping are yielded in the order they appear in source."
>>
>>This was certainly the behaviour in my tests - i.e. invoice amounts were in the right order, clients ordered by descending largest invoice.
>
>Your previous attempt with the two queries might have worked then if it was brought locally (AsEnumerable or ToList) before the grouping. As is, it was left as an IQueryable so the group by was probably executed on the server as a new query rather than using the previous results. The GroupBy probably also forced a new ordering.

Looks like your last sentence identified the problem. This query:
var q3 = Context.Invoices.Where(x => x.invoiceamount <= invoiceFilterAmount)
.OrderByDescending(x => x.invoiceamount)
.GroupBy(x => x.clientid).ToList();
generated the following SQL Query:
SELECT 
[Project2].[clientid] AS [clientid], 
[Project2].[C1] AS [C1], 
[Project2].[Id] AS [Id], 
[Project2].[clientid1] AS [clientid1], 
[Project2].[invoiceamount] AS [invoiceamount]
FROM ( SELECT 
	[Distinct1].[clientid] AS [clientid], 
	[Extent2].[Id] AS [Id], 
	[Extent2].[clientid] AS [clientid1], 
	[Extent2].[invoiceamount] AS [invoiceamount], 
	CASE WHEN ([Extent2].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
	FROM   (SELECT DISTINCT 
		[Extent1].[clientid] AS [clientid]
		FROM [dbo].[Invoices] AS [Extent1]
		WHERE [Extent1].[invoiceamount] <= cast(20000 as float(53)) ) AS [Distinct1]
	LEFT OUTER JOIN [dbo].[Invoices] AS [Extent2]
	ON ([Extent2].[invoiceamount] <= cast(20000 as float(53))) AND ([Distinct1].[clientid] = [Extent2].[clientid])
)  AS [Project2]
ORDER BY [Project2].[clientid] ASC, [Project2].[C1] ASC
No ordering by invoiceamount there. By contrast the query:
var query2 = Context.Invoices.GroupBy(x => x.clientid)
.Select(group => new { Id = group.Key, invs = group.OrderByDescending(x => x.invoiceamount) })
.OrderByDescending(group => group.invs.Sum(x => x.invoiceamount));
produced:
SELECT 
[Project3].[clientid] AS [clientid], 
[Project3].[C1] AS [C1], 
[Project3].[Id] AS [Id], 
[Project3].[clientid1] AS [clientid1], 
[Project3].[invoiceamount] AS [invoiceamount]
FROM ( SELECT 
	[Project2].[clientid] AS [clientid], 
	[Extent3].[Id] AS [Id], 
	[Extent3].[clientid] AS [clientid1], 
	[Extent3].[invoiceamount] AS [invoiceamount], 
	CASE WHEN ([Extent3].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1], 
	[Project2].[C1] AS [C2]
	FROM   (SELECT 
		[Distinct1].[clientid] AS [clientid], 
		(SELECT 
			SUM([Extent2].[invoiceamount]) AS [A1]
			FROM [dbo].[Invoices] AS [Extent2]
			WHERE [Distinct1].[clientid] = [Extent2].[clientid]) AS [C1]
		FROM ( SELECT DISTINCT 
			[Extent1].[clientid] AS [clientid]
			FROM [dbo].[Invoices] AS [Extent1]
		)  AS [Distinct1] ) AS [Project2]
	LEFT OUTER JOIN [dbo].[Invoices] AS [Extent3] ON [Project2].[clientid] = [Extent3].[clientid]
)  AS [Project3]
ORDER BY [Project3].[C2] DESC, [Project3].[clientid] ASC, [Project3].[C1] ASC, [Project3].[invoiceamount] DESC
which gives correct results.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform