Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Peter S 64 posts 106 karma points
    Oct 28, 2009 @ 17:17
    Peter S
    0

    Sorting of page.Children in C#

    Hi,

    Is there an easy way to sort the children of a node when using custom user controls in C#? In the demo videos I've seen sorting is easily done in XSLT, but I haven't been able to find a way to do this in C#.

    This is the code I'm using to loop through the childnodes:

     Node curPage = Node.GetCurrent();
     foreach (Node page in curPage.Children)
    {
    //do something
    }

     

  • Chris Koiak 700 posts 2626 karma points
    Oct 28, 2009 @ 18:51
    Chris Koiak
    1

    I would suggest you look into Linq2Umbraco. In it's simplest form it's a linq wrapper for accessing the node/xml objects.

    <!-- /* Font Definitions */ @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-alt:"Calisto MT"; mso-font-charset:0; mso-generic-font-family:roman; mso-font-pitch:variable; mso-font-signature:-1610611985 1107304683 0 0 415 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-alt:"Times New Roman"; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-520092929 1073786111 9 0 415 0;} @font-face {font-family:Consolas; panose-1:2 11 6 9 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:-520092929 1073806591 9 0 415 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:Calibri; mso-fareast-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-fareast-language:EN-US;} a:link, span.MsoHyperlink {mso-style-priority:99; color:blue; mso-themecolor:hyperlink; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {mso-style-noshow:yes; mso-style-priority:99; color:purple; mso-themecolor:followedhyperlink; text-decoration:underline; text-underline:single;} p.MsoPlainText, li.MsoPlainText, div.MsoPlainText {mso-style-noshow:yes; mso-style-priority:99; mso-style-link:"Plain Text Char"; margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.5pt; font-family:Consolas; mso-fareast-font-family:Calibri; mso-fareast-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-fareast-language:EN-US;} span.PlainTextChar {mso-style-name:"Plain Text Char"; mso-style-noshow:yes; mso-style-priority:99; mso-style-unhide:no; mso-style-locked:yes; mso-style-link:"Plain Text"; mso-ansi-font-size:10.5pt; mso-bidi-font-size:10.5pt; font-family:Consolas; mso-ascii-font-family:Consolas; mso-hansi-font-family:Consolas;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:Calibri; mso-fareast-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-fareast-language:EN-US;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 72.0pt 72.0pt 72.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->

    http://www.aaron-powell.com/blog/october-2009/a-developers-guide-to-linq-to-umbraco---part-0.aspx

     

    You'll be able to use the full power of linq, including sorting.

     

  • Peter S 64 posts 106 karma points
    Oct 28, 2009 @ 20:12
    Peter S
    0

    Thanks. I'll give that a try. Was afraid I had to wait till Umbraco 4.1 to be able to use Linq :)

  • Peter S 64 posts 106 karma points
    Oct 28, 2009 @ 20:50
    Peter S
    0

    Or is this only working with version 4.1? In that case I might have to upgrade from the current 4.0 version...

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Oct 28, 2009 @ 22:08
    Dirk De Grave
    0

    Last assumption is the correct one, Linq2umbraco only from v4.1 release on.

     

    Cheers,

    /Dirk

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Oct 28, 2009 @ 22:10
    Dirk De Grave
    0

    Although some spicy lambda expression would do great as well.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Oct 28, 2009 @ 22:33
    Aaron Powell
    2

    I strongly recommend you do not enumorate through the Children collection of an object in v4.0.

    It's not a huge problem if you're using Node, but if you're using anything such as Media, Document, DocumentType, etc you will access the DB for every single iteration, since it returns an array.

    It's always best to store the output of the property in a local variable before iterating.

    4.1 has addressed this child problem *to a certain degree*.

    But LINQ to Umbraco is just so much hotter ;)

  • Peter S 64 posts 106 karma points
    Oct 29, 2009 @ 12:05
    Peter S
    0

    /cheer

    Man, this is so much better. Took me a little while to get it working, because I just had the website running on our server that didn't have Visual Studio installed, so I kept getting the ReflectionTypeLoadException. But now I have it all working, and I can sort with Linq.

    Thanks all. And yes, Linq is hot :)TTitle is required

  • Chad Rosenthal 272 posts 474 karma points
    Nov 04, 2009 @ 18:52
    Chad Rosenthal
    0

    Just out of curiosity, if Linq is not an option for us, is there a way to sort items in c#?

    I'm mainly dealing with extending the autofolders program. The problem is that when you create a new folder, it isn't always in date order. The folders are created in the order that they were created and not necessarily in date order. It would be nice that when it adds in, that it automatically sorts them correctly.

    Problem is, I'm trying to do it in a 2.0 environment. I know that you can add Linq to 2.0 without updating to 3.5 by adding the assemblies to your project, but I am curious if it can be done without Linq.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Nov 04, 2009 @ 22:42
    Aaron Powell
    1

    Arrays are sortable, they always have been - http://msdn.microsoft.com/en-us/library/system.array.sort.aspx

    But as I said, make sure you create a local variable otherwise you'll be doing a hell of a lot of db access

  • dennish 17 posts 41 karma points
    Dec 04, 2009 @ 15:38
    dennish
    1

    DataView dataView = new DataView(node.ChildrenAsTable());
    dataView.Sort = " Date ASC";
    foreach (DataRowView view in dataView)

    {
          Response.Write(view["NodeName"] + "<br />");
    }

     

Please Sign in or register to post replies

Write your reply to:

Draft