ARTICLE AD BOX
I have a method, that returns a value of type List<IGrouping<PGGroupingKey, PGroupingResult>>.
The request code and description of the types are below
internal class PDGrouppingKey { public int? Code { get; set; } public int? RevokeCode { get; set; } } internal class PDGroupingResult { public int? Code { get; set; } public int? RevokeCode { get; set; } public PersonData PersonData { get; set; } } ctx.VerifyData.Where(v => v.ProjectID == progetcId && (v.RevokeCode > 11 && v.RevokeCode < 21)) .Join(ctx.PersonData, v => v.PersonDataID, p => p.PersonDataID, (v, p) => new PDGroupingResult { Code = p.RegionCode, RevokeCode = v.RevokeCode, PersonData = p }) .OrderBy(o => o.Code) .ThenBy(o => o.PersonData.FirstName) .ThenBy(o => o.PersonData.LastName) .ThenBy(o => o.PersonData.ParentName) .GroupBy(p => new PDGrouppingKey { Code = p.Code, RevokeCode = p.RevokeCode }) .ToList();The result is incorrect, sorted only by the 'Code' field. And the request itself is incorrect. There is no sorting by fields
o.PersonData.FirstName, o.PersonData.LastName, o.PersonData.ParentNameAnd here is the query that LINQ generates
exec sp_executesql N'SELECT [Project2].[C1] AS [C1], [Project2].[RegionCode] AS [RegionCode], [Project2].[RevokeCode] AS [RevokeCode], [Project2].[C2] AS [C2], [Project2].[PersonDataID] AS [PersonDataID], [Project2].[RegionCode1] AS [RegionCode1], [Project2].[RevokeCode1] AS [RevokeCode1], [Project2].[CreateDate] AS [CreateDate], [Project2].[CreatorID] AS [CreatorID], [Project2].[ModifyDate] AS [ModifyDate], [Project2].[ModifierID] AS [ModifierID], [Project2].[StatusCode] AS [StatusCode], [Project2].[LastName] AS [LastName], [Project2].[FirstName] AS [FirstName], [Project2].[ParentName] AS [ParentName], [Project2].[Gender] AS [Gender], [Project2].[Birthday] AS [Birthday], [Project2].[DepartCode] AS [DepartCode], [Project2].[DeliveryCode] AS [DeliveryCode], [Project2].[PostIndexN] AS [PostIndexN], [Project2].[Region1] AS [Region1], [Project2].[RegionType1] AS [RegionType1], [Project2].[Region2] AS [Region2], [Project2].[RegionType2] AS [RegionType2], [Project2].[Cluster] AS [Cluster], [Project2].[Street] AS [Street], [Project2].[House] AS [House], [Project2].[Building] AS [Building], [Project2].[Flat] AS [Flat], [Project2].[RemarkCode] AS [RemarkCode], [Project2].[RemarkText] AS [RemarkText], [Project2].[RemarkDate] AS [RemarkDate], [Project2].[CategoryCode] AS [CategoryCode], [Project2].[AwardCode] AS [AwardCode] FROM ( SELECT [Distinct1].[RevokeCode] AS [RevokeCode], [Distinct1].[RegionCode] AS [RegionCode], [Distinct1].[C1] AS [C1], [Join2].[RevokeCode] AS [RevokeCode1], [Join2].[PersonDataID1] AS [PersonDataID], [Join2].[CreateDate1] AS [CreateDate], [Join2].[CreatorID1] AS [CreatorID], [Join2].[ModifyDate1] AS [ModifyDate], [Join2].[ModifierID1] AS [ModifierID], [Join2].[StatusCode1] AS [StatusCode], [Join2].[LastName] AS [LastName], [Join2].[FirstName] AS [FirstName], [Join2].[ParentName] AS [ParentName], [Join2].[Gender] AS [Gender], [Join2].[Birthday] AS [Birthday], [Join2].[DepartCode1] AS [DepartCode], [Join2].[RegionCode] AS [RegionCode1], [Join2].[DeliveryCode] AS [DeliveryCode], [Join2].[PostIndexN] AS [PostIndexN], [Join2].[Region1] AS [Region1], [Join2].[RegionType1] AS [RegionType1], [Join2].[Region2] AS [Region2], [Join2].[RegionType2] AS [RegionType2], [Join2].[Cluster] AS [Cluster], [Join2].[Street] AS [Street], [Join2].[House] AS [House], [Join2].[Building] AS [Building], [Join2].[Flat] AS [Flat], [Join2].[RemarkCode1] AS [RemarkCode], [Join2].[RemarkText1] AS [RemarkText], [Join2].[RemarkDate] AS [RemarkDate], [Join2].[CategoryCode1] AS [CategoryCode], [Join2].[AwardCode1] AS [AwardCode], CASE WHEN ([Join2].[PersonDataID1] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2] FROM (SELECT DISTINCT [Extent1].[RevokeCode] AS [RevokeCode], [Extent2].[RegionCode] AS [RegionCode], 1 AS [C1] FROM [dbo].[VerifyData] AS [Extent1] WITH(NOLOCK) INNER JOIN [dbo].[PersonData] AS [Extent2] WITH(NOLOCK) ON [Extent1].[PersonDataID] = [Extent2].[PersonDataID] WHERE ([Extent1].[ProjectID] = @p__linq__0) AND ([Extent1].[RevokeCode] > 11) AND ([Extent1].[RevokeCode] < 21) ) AS [Distinct1] LEFT OUTER JOIN (SELECT [Extent3].[RevokeCode] AS [RevokeCode], [Extent3].[ProjectID] AS [ProjectID], [Extent4].[PersonDataID] AS [PersonDataID1], [Extent4].[CreateDate] AS [CreateDate1], [Extent4].[CreatorID] AS [CreatorID1], [Extent4].[ModifyDate] AS [ModifyDate1], [Extent4].[ModifierID] AS [ModifierID1], [Extent4].[StatusCode] AS [StatusCode1], [Extent4].[LastName] AS [LastName], [Extent4].[FirstName] AS [FirstName], [Extent4].[ParentName] AS [ParentName], [Extent4].[Gender] AS [Gender], [Extent4].[Birthday] AS [Birthday], [Extent4].[DepartCode] AS [DepartCode1], [Extent4].[RegionCode] AS [RegionCode], [Extent4].[DeliveryCode] AS [DeliveryCode], [Extent4].[PostIndexN] AS [PostIndexN], [Extent4].[Region1] AS [Region1], [Extent4].[RegionType1] AS [RegionType1], [Extent4].[Region2] AS [Region2], [Extent4].[RegionType2] AS [RegionType2], [Extent4].[Cluster] AS [Cluster], [Extent4].[Street] AS [Street], [Extent4].[House] AS [House], [Extent4].[Building] AS [Building], [Extent4].[Flat] AS [Flat], [Extent4].[RemarkCode] AS [RemarkCode1], [Extent4].[RemarkText] AS [RemarkText1], [Extent4].[RemarkDate] AS [RemarkDate], [Extent4].[CategoryCode] AS [CategoryCode1], [Extent4].[AwardCode] AS [AwardCode1] FROM [dbo].[VerifyData] AS [Extent3] WITH(NOLOCK) INNER JOIN [dbo].[PersonData] AS [Extent4] WITH(NOLOCK) ON [Extent3].[PersonDataID] = [Extent4].[PersonDataID] ) AS [Join2] ON ([Join2].[ProjectID] = @p__linq__0) AND ([Join2].[RevokeCode] > 11) AND ([Join2].[RevokeCode] < 21) AND (([Distinct1].[RegionCode] = [Join2].[RegionCode]) OR (([Distinct1].[RegionCode] IS NULL) AND ([Join2].[RegionCode] IS NULL))) AND (([Distinct1].[RevokeCode] = [Join2].[RevokeCode]) OR (([Distinct1].[RevokeCode] IS NULL) AND ([Join2].[RevokeCode] IS NULL))) ) AS [Project2] ORDER BY [Project2].[C1] ASC, [Project2].[RegionCode] ASC, [Project2].[RevokeCode] ASC, [Project2].[C2] ASC',N'@p__linq__0 int',@p__linq__0=63What am I doing wrong, and how to rewrite the query? Of course, I can write everything in T-SQL... but I would like to use LINQ
