{"id":1998,"date":"2015-03-18T18:03:09","date_gmt":"2015-03-18T16:03:09","guid":{"rendered":"http:\/\/soci.hu\/blog\/?p=1998"},"modified":"2015-03-18T18:03:09","modified_gmt":"2015-03-18T16:03:09","slug":"ef-gyerek-kollekcio-rendezes","status":"publish","type":"post","link":"https:\/\/soci.hu\/blog\/index.php\/2015\/03\/18\/ef-gyerek-kollekcio-rendezes\/","title":{"rendered":"EF gyerek kollekci\u00f3 rendez\u00e9s"},"content":{"rendered":"<p>N\u00e9ha szeretn\u00e9nk nem csak egy entit\u00e1s list\u00e1t, hanem annak gyerekeit is rendeztetni, azaz az egy sz\u00fcl\u0151 al\u00e1 tartoz\u00f3 gyerekeket order by-olni.<\/p>\n<p>Egy lehets\u00e9ges megold\u00e1s:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing (var c = new EdbContext())\r\n{\r\n    var jobs = c.LoaderJobs\r\n        .Include(&quot;LoaderJobSteps&quot;)\r\n        .Include(&quot;LoaderJobSteps.ExtractPathLocation&quot;)\r\n        .Include(&quot;LoaderJobSteps.FormatFilePathLocation&quot;)\r\n        .OrderBy(j =&gt; j.ExecutionOrder).ToList();\r\n\r\n    \/\/A bit complicated to be able to order LoaderJobSteps properly\r\n    var x = jobs.Select(job =&gt; new { J = job, JS = job.LoaderJobSteps.OrderBy(js =&gt; js.ExecutionOrder).ToList() });\r\n\r\n    return x.Select(f =&gt; f.J).ToList();\r\n}\r\n<\/pre>\n<p>Sz\u00e1nd\u00e9kaim szerint a gyerekkollekci\u00f3k order by-\u00e1t is az adatb\u00e1zissal v\u00e9geztettem volna, de j\u00f3l l\u00e1that\u00f3 a k\u00f3db\u00f3l, hogy itt .netb\u0151l t\u00f6rt\u00e9nik a gyerekek (LoaderJobSteps) rendez\u00e9se.<br \/>\nHa kiveszem az els\u0151 ToList()-et, akkor az EF helyesen \u00e1ttolja a 2. order by-t is az SQL Serverre, de akkor meg nem t\u00f6lti be az unok\u00e1kat (LoaderJobSteps.ExtractPathLocation).<br \/>\nEbben a p\u00e9ld\u00e1ban nincs jelent\u0151s\u00e9ge hol rendezek, de ha valaki tudja, mit\u0151l nem megy ilyenkor az Include, \u00e9rdekelne a megold\u00e1s.<\/p>\n<p>Ha benn van az els\u0151 ToList(), akkor az SQL ok, benne van minden eagerly loaded entit\u00e1s, de nincs benne order by a gyerekekre, az a LINQ2Objects fogja v\u00e9grehajtani. (Extent2-re nincs order by).<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT \r\n    &#x5B;Project1].&#x5B;Id] AS &#x5B;Id], \r\n    &#x5B;Project1].&#x5B;Description] AS &#x5B;Description], \r\n    &#x5B;Project1].&#x5B;ExecutionOrder] AS &#x5B;ExecutionOrder], \r\n    &#x5B;Project1].&#x5B;C1] AS &#x5B;C1], \r\n    &#x5B;Project1].&#x5B;Id1] AS &#x5B;Id1], \r\n    &#x5B;Project1].&#x5B;StepName] AS &#x5B;StepName], \r\n    &#x5B;Project1].&#x5B;ExecutionOrder1] AS &#x5B;ExecutionOrder1], \r\n    &#x5B;Project1].&#x5B;ProcedureNamePrepare] AS &#x5B;ProcedureNamePrepare], \r\n    &#x5B;Project1].&#x5B;ProcedureNameImport] AS &#x5B;ProcedureNameImport], \r\n    &#x5B;Project1].&#x5B;ProcedureNameLoad] AS &#x5B;ProcedureNameLoad], \r\n    &#x5B;Project1].&#x5B;ExtractFileName] AS &#x5B;ExtractFileName], \r\n    &#x5B;Project1].&#x5B;FailOnMissingFile] AS &#x5B;FailOnMissingFile], \r\n    &#x5B;Project1].&#x5B;FormatFileName] AS &#x5B;FormatFileName], \r\n    &#x5B;Project1].&#x5B;Id2] AS &#x5B;Id2], \r\n    &#x5B;Project1].&#x5B;FolderPath] AS &#x5B;FolderPath], \r\n    &#x5B;Project1].&#x5B;Id3] AS &#x5B;Id3], \r\n    &#x5B;Project1].&#x5B;FolderPath1] AS &#x5B;FolderPath1], \r\n    &#x5B;Project1].&#x5B;LoaderJobId] AS &#x5B;LoaderJobId]\r\n    FROM ( SELECT \r\n        &#x5B;Extent1].&#x5B;Id] AS &#x5B;Id], \r\n        &#x5B;Extent1].&#x5B;Description] AS &#x5B;Description], \r\n        &#x5B;Extent1].&#x5B;ExecutionOrder] AS &#x5B;ExecutionOrder], \r\n        &#x5B;Join2].&#x5B;Id1] AS &#x5B;Id1], \r\n        &#x5B;Join2].&#x5B;StepName] AS &#x5B;StepName], \r\n        &#x5B;Join2].&#x5B;ExecutionOrder] AS &#x5B;ExecutionOrder1], \r\n        &#x5B;Join2].&#x5B;ProcedureNamePrepare] AS &#x5B;ProcedureNamePrepare], \r\n        &#x5B;Join2].&#x5B;ProcedureNameImport] AS &#x5B;ProcedureNameImport], \r\n        &#x5B;Join2].&#x5B;ProcedureNameLoad] AS &#x5B;ProcedureNameLoad], \r\n        &#x5B;Join2].&#x5B;ExtractFileName] AS &#x5B;ExtractFileName], \r\n        &#x5B;Join2].&#x5B;FailOnMissingFile] AS &#x5B;FailOnMissingFile], \r\n        &#x5B;Join2].&#x5B;FormatFileName] AS &#x5B;FormatFileName], \r\n        &#x5B;Join2].&#x5B;LoaderJobId] AS &#x5B;LoaderJobId], \r\n        &#x5B;Join2].&#x5B;Id2] AS &#x5B;Id2], \r\n        &#x5B;Join2].&#x5B;FolderPath1] AS &#x5B;FolderPath], \r\n        &#x5B;Join2].&#x5B;Id3] AS &#x5B;Id3], \r\n        &#x5B;Join2].&#x5B;FolderPath2] AS &#x5B;FolderPath1], \r\n        CASE WHEN (&#x5B;Join2].&#x5B;Id1] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS &#x5B;C1]\r\n        FROM  &#x5B;dbo].&#x5B;LoaderJob] AS &#x5B;Extent1]\r\n        LEFT OUTER JOIN  (SELECT &#x5B;Extent2].&#x5B;Id] AS &#x5B;Id1], &#x5B;Extent2].&#x5B;StepName] AS &#x5B;StepName], &#x5B;Extent2].&#x5B;ExecutionOrder] AS &#x5B;ExecutionOrder], &#x5B;Extent2].&#x5B;ProcedureNamePrepare] AS &#x5B;ProcedureNamePrepare], &#x5B;Extent2].&#x5B;ProcedureNameImport] AS &#x5B;ProcedureNameImport], &#x5B;Extent2].&#x5B;ProcedureNameLoad] AS &#x5B;ProcedureNameLoad], &#x5B;Extent2].&#x5B;ExtractFileName] AS &#x5B;ExtractFileName], &#x5B;Extent2].&#x5B;FailOnMissingFile] AS &#x5B;FailOnMissingFile], &#x5B;Extent2].&#x5B;FormatFileName] AS &#x5B;FormatFileName], &#x5B;Extent2].&#x5B;LoaderJobId] AS &#x5B;LoaderJobId], &#x5B;Extent3].&#x5B;Id] AS &#x5B;Id2], &#x5B;Extent3].&#x5B;FolderPath] AS &#x5B;FolderPath1], &#x5B;Extent4].&#x5B;Id] AS &#x5B;Id3], &#x5B;Extent4].&#x5B;FolderPath] AS &#x5B;FolderPath2]\r\n            FROM   &#x5B;dbo].&#x5B;LoaderJobStep] AS &#x5B;Extent2]\r\n            LEFT OUTER JOIN &#x5B;dbo].&#x5B;PathLocation] AS &#x5B;Extent3] ON &#x5B;Extent2].&#x5B;ExtractPathLocationId] = &#x5B;Extent3].&#x5B;Id]\r\n            LEFT OUTER JOIN &#x5B;dbo].&#x5B;PathLocation] AS &#x5B;Extent4] ON &#x5B;Extent2].&#x5B;FormatFilePathLocationId] = &#x5B;Extent4].&#x5B;Id] ) AS &#x5B;Join2] ON &#x5B;Extent1].&#x5B;Id] = &#x5B;Join2].&#x5B;LoaderJobId]\r\n    )  AS &#x5B;Project1]\r\n    ORDER BY &#x5B;Project1].&#x5B;ExecutionOrder] ASC, &#x5B;Project1].&#x5B;Id] ASC, &#x5B;Project1].&#x5B;C1] ASC\r\n<\/pre>\n<p>Az els\u0151 ToList() n\u00e9lk\u00fcl:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT \r\n    &#x5B;Extent1].&#x5B;Id] AS &#x5B;Id], \r\n    &#x5B;Extent1].&#x5B;Description] AS &#x5B;Description], \r\n    &#x5B;Extent1].&#x5B;ExecutionOrder] AS &#x5B;ExecutionOrder]\r\n    FROM &#x5B;dbo].&#x5B;LoaderJob] AS &#x5B;Extent1]\r\n    ORDER BY &#x5B;Extent1].&#x5B;ExecutionOrder] ASC\r\n<\/pre>\n<p>Hm. Tippek? <\/p>\n<p>K\u00f6zben r\u00e1j\u00f6ttem, megv\u00e1laszolom magamnak a k\u00e9rd\u00e9st. :) A v\u00e9g\u00e9re kell rakni az include-okat:<\/p>\n<p>using (var c = new EdbContext())<br \/>\n{<br \/>\n    var jobs = c.LoaderJobs<br \/>\n        .OrderBy(j => j.ExecutionOrder);<\/p>\n<p>    \/\/A bit complicated to be able to order LoaderJobSteps properly<br \/>\n    var x = jobs.Select(job => new { J = job, JS = job.LoaderJobSteps.OrderBy(js => js.ExecutionOrder) });<\/p>\n<p>    return x.Select(f => f.J).Include(&#8220;LoaderJobSteps&#8221;)<br \/>\n        .Include(&#8220;LoaderJobSteps.ExtractPathLocation&#8221;)<br \/>\n        .Include(&#8220;LoaderJobSteps.FormatFilePathLocation&#8221;).ToList();<br \/>\n}<br \/>\n[\/source]<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT \r\n    &#x5B;Project1].&#x5B;Id] AS &#x5B;Id], \r\n    &#x5B;Project1].&#x5B;Description] AS &#x5B;Description], \r\n    &#x5B;Project1].&#x5B;ExecutionOrder] AS &#x5B;ExecutionOrder], \r\n    &#x5B;Project1].&#x5B;C1] AS &#x5B;C1], \r\n    &#x5B;Project1].&#x5B;Id1] AS &#x5B;Id1], \r\n    &#x5B;Project1].&#x5B;StepName] AS &#x5B;StepName], \r\n    &#x5B;Project1].&#x5B;ExecutionOrder1] AS &#x5B;ExecutionOrder1], \r\n    &#x5B;Project1].&#x5B;ProcedureNamePrepare] AS &#x5B;ProcedureNamePrepare], \r\n    &#x5B;Project1].&#x5B;ProcedureNameImport] AS &#x5B;ProcedureNameImport], \r\n    &#x5B;Project1].&#x5B;ProcedureNameLoad] AS &#x5B;ProcedureNameLoad], \r\n    &#x5B;Project1].&#x5B;ExtractFileName] AS &#x5B;ExtractFileName], \r\n    &#x5B;Project1].&#x5B;FailOnMissingFile] AS &#x5B;FailOnMissingFile], \r\n    &#x5B;Project1].&#x5B;FormatFileName] AS &#x5B;FormatFileName], \r\n    &#x5B;Project1].&#x5B;Id2] AS &#x5B;Id2], \r\n    &#x5B;Project1].&#x5B;FolderPath] AS &#x5B;FolderPath], \r\n    &#x5B;Project1].&#x5B;Id3] AS &#x5B;Id3], \r\n    &#x5B;Project1].&#x5B;FolderPath1] AS &#x5B;FolderPath1], \r\n    &#x5B;Project1].&#x5B;LoaderJobId] AS &#x5B;LoaderJobId]\r\n    FROM ( SELECT \r\n        &#x5B;Extent1].&#x5B;Id] AS &#x5B;Id], \r\n        &#x5B;Extent1].&#x5B;Description] AS &#x5B;Description], \r\n        &#x5B;Extent1].&#x5B;ExecutionOrder] AS &#x5B;ExecutionOrder], \r\n        &#x5B;Join2].&#x5B;Id1] AS &#x5B;Id1], \r\n        &#x5B;Join2].&#x5B;StepName] AS &#x5B;StepName], \r\n        &#x5B;Join2].&#x5B;ExecutionOrder] AS &#x5B;ExecutionOrder1], \r\n        &#x5B;Join2].&#x5B;ProcedureNamePrepare] AS &#x5B;ProcedureNamePrepare], \r\n        &#x5B;Join2].&#x5B;ProcedureNameImport] AS &#x5B;ProcedureNameImport], \r\n        &#x5B;Join2].&#x5B;ProcedureNameLoad] AS &#x5B;ProcedureNameLoad], \r\n        &#x5B;Join2].&#x5B;ExtractFileName] AS &#x5B;ExtractFileName], \r\n        &#x5B;Join2].&#x5B;FailOnMissingFile] AS &#x5B;FailOnMissingFile], \r\n        &#x5B;Join2].&#x5B;FormatFileName] AS &#x5B;FormatFileName], \r\n        &#x5B;Join2].&#x5B;LoaderJobId] AS &#x5B;LoaderJobId], \r\n        &#x5B;Join2].&#x5B;Id2] AS &#x5B;Id2], \r\n        &#x5B;Join2].&#x5B;FolderPath1] AS &#x5B;FolderPath], \r\n        &#x5B;Join2].&#x5B;Id3] AS &#x5B;Id3], \r\n        &#x5B;Join2].&#x5B;FolderPath2] AS &#x5B;FolderPath1], \r\n        CASE WHEN (&#x5B;Join2].&#x5B;Id1] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS &#x5B;C1]\r\n        FROM  &#x5B;dbo].&#x5B;LoaderJob] AS &#x5B;Extent1]\r\n        LEFT OUTER JOIN  (SELECT &#x5B;Extent2].&#x5B;Id] AS &#x5B;Id1], &#x5B;Extent2].&#x5B;StepName] AS &#x5B;StepName], &#x5B;Extent2].&#x5B;ExecutionOrder] AS &#x5B;ExecutionOrder], &#x5B;Extent2].&#x5B;ProcedureNamePrepare] AS &#x5B;ProcedureNamePrepare], &#x5B;Extent2].&#x5B;ProcedureNameImport] AS &#x5B;ProcedureNameImport], &#x5B;Extent2].&#x5B;ProcedureNameLoad] AS &#x5B;ProcedureNameLoad], &#x5B;Extent2].&#x5B;ExtractFileName] AS &#x5B;ExtractFileName], &#x5B;Extent2].&#x5B;FailOnMissingFile] AS &#x5B;FailOnMissingFile], &#x5B;Extent2].&#x5B;FormatFileName] AS &#x5B;FormatFileName], &#x5B;Extent2].&#x5B;LoaderJobId] AS &#x5B;LoaderJobId], &#x5B;Extent3].&#x5B;Id] AS &#x5B;Id2], &#x5B;Extent3].&#x5B;FolderPath] AS &#x5B;FolderPath1], &#x5B;Extent4].&#x5B;Id] AS &#x5B;Id3], &#x5B;Extent4].&#x5B;FolderPath] AS &#x5B;FolderPath2]\r\n            FROM   &#x5B;dbo].&#x5B;LoaderJobStep] AS &#x5B;Extent2]\r\n            LEFT OUTER JOIN &#x5B;dbo].&#x5B;PathLocation] AS &#x5B;Extent3] ON &#x5B;Extent2].&#x5B;ExtractPathLocationId] = &#x5B;Extent3].&#x5B;Id]\r\n            LEFT OUTER JOIN &#x5B;dbo].&#x5B;PathLocation] AS &#x5B;Extent4] ON &#x5B;Extent2].&#x5B;FormatFilePathLocationId] = &#x5B;Extent4].&#x5B;Id] ) AS &#x5B;Join2] ON &#x5B;Extent1].&#x5B;Id] = &#x5B;Join2].&#x5B;LoaderJobId]\r\n    )  AS &#x5B;Project1]\r\n    ORDER BY &#x5B;Project1].&#x5B;ExecutionOrder] ASC, &#x5B;Project1].&#x5B;Id] ASC, &#x5B;Project1].&#x5B;C1] ASC\r\n<\/pre>\n<p>Hm, m\u00e9gse j\u00f3, nincs benne az sql-ben a 2. order by. Sz\u00f3val a k\u00e9rd\u00e9s nyitott, \u00e1t lehet tolni adatb\u00e1zis oldalra a child kollekci\u00f3 order by-t?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>N\u00e9ha szeretn\u00e9nk nem csak egy entit\u00e1s list\u00e1t, hanem annak gyerekeit is rendeztetni, azaz az egy sz\u00fcl\u0151 al\u00e1 tartoz\u00f3 gyerekeket order by-olni. Egy&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,10,6,74,4,30],"tags":[],"class_list":["post-1998","post","type-post","status-publish","format-standard","hentry","category-net","category-c","category-adatbazisok","category-entity-framework","category-szakmai-elet","category-sql-server"],"_links":{"self":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1998"}],"version-history":[{"count":2,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1998\/revisions"}],"predecessor-version":[{"id":2000,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1998\/revisions\/2000"}],"wp:attachment":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}