More in this section

Forums / Developing with Sitefinity / Using OrderBy with fluent API

Using OrderBy with fluent API

5 posts, 1 answered
  1. Scott Rozman
    Scott Rozman avatar
    93 posts
    Registered:
    10 Oct 2012
    23 Nov 2011
    Link to this post
    I developed a slideshow widget that uses images from a particular album and I need a way to control the order the slides are displayed.
    I have tried, unsuccessfully, to use OrderBy and use Titles of slide01, slide02 etc.
    Can anyone see what I am missing? The OrderBy seems to have no effect...

    public DataTable GetImages()
            {
                string title = this.AlbumTitle;
                DataTable ImagesTable = new DataTable("ImagesTable");
                ImagesTable.Columns.Add("PhotoUrl", typeof(string));
                ImagesTable.Columns.Add("Title", typeof(string));
                ImagesTable.Columns.Add("AltText", typeof(string));
     
                //get IQueryable of images from the Fluent API.
                var images = App.WorkWith().Images()
                    .Where(
                    (w) => w.Parent.Title == title &&
                            w.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live)
                        .OrderBy(w => w.Title)
                        .Get();
                foreach (Telerik.Sitefinity.Libraries.Model.Image v in images)
                {
                    string photoUrl = v.MediaUrl;
                    ImagesTable.Rows.Add(new string[] { photoUrl, v.Title.ToString(), v.AlternativeText.ToString() });
                }
     
                return ImagesTable;
            }
  2. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    23 Nov 2011
    Link to this post
    Hi Scott,

    Shouldn't you just do the Get() first and then the OrderBy?
    Or like this:

    foreach (Telerik.Sitefinity.Libraries.Model.Image v in images.OrderBy(w=>w.Title)
    {
       string photoUrl = v.MediaUrl;
       ImagesTable.Rows.Add(new string[] { photoUrl, v.Title.ToString(), v.AlternativeText.ToString() });
    }
      
    return ImagesTable;

    Regards,
    Daniel
    Answered
  3. Scott Rozman
    Scott Rozman avatar
    93 posts
    Registered:
    10 Oct 2012
    28 Nov 2011
    Link to this post
    Thanks, Daniel! That works great...
  4. PlanetMagpie
    PlanetMagpie avatar
    2 posts
    Registered:
    10 Jun 2009
    12 Apr 2012
    Link to this post
    I have a bit column that I wanted to order by values of true or 1 in this column.  Is there a  way to get the name of the dynamic module field for the OrderBy directive? .OrderBy(z => z.WhatHere)
    In my case the column is named Default.  


  5. Dan Sorensen
    Dan Sorensen avatar
    256 posts
    Registered:
    13 Aug 2010
    16 Apr 2012
    Link to this post
    W3: It's a slightly different process for Dynamic Modules than it is for built in modules such as Albums. I'm still new to working with Dynamic Modules, but I think this will work for you:

    // required for GetValue()
    using Telerik.Sitefinity.Model;

    .OrderBy(d => d.GetValue(
    "Default"))

    Since it is a dynamic field, you cannot reference it as you would a strongly typed field (e.g. d.Default), instead you have to use a method such as GetValue() that can resolve the dynamic name for you.

    Note that GetValue won't return the expected result if you are testing a dynamic field that was populated with a drop down. That's a different quirky process that is more suitable for a different discussion thread.
5 posts, 1 answered