Copied to clipboard

Flag this post as spam?

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


  • Greg Jenson 24 posts 157 karma points
    Dec 06, 2018 @ 22:17
    Greg  Jenson
    0

    LINQ OrderBy breaking my page

    var ts = Model.Content.Site().FirstChild("trainingSchedule").Children("trainingScheduleItem")
    .Where(x => x.IsVisible())
    .Where(x => x.GetPropertyValue("programKey").ToString() == pk)
    .GroupBy(x => x.GetPropertyValue("grouping"))
      .OrderBy(x => x.GetPropertyValue<DateTime>("startDate")); // This line is breaking the page - why?
    

    This was created using the query builder tool - I added an additional .Where clause and the GroupBy and everything works great, until I add the .OrderBy. Now I'm getting the message:

    CS1928: 'System.Linq.IGrouping<object,Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'GetPropertyValue' and the best extension method...
    

    Can anyone tell me what I'm missing?

    UPDATE: Moved the OrderBy clause to above the GroupBy... new and working code below. Took me 6+ hours to figure that out - maybe this will help someone else.

    var ts = Model.Content.Site().FirstChild("trainingSchedule").Children("trainingScheduleItem")
        .Where(x => x.IsVisible())
        .Where(x => x.GetPropertyValue("programKey").ToString() == pk)
        .OrderBy(x => x.GetPropertyValue<DateTime>("startDate")
        .GroupBy(x => x.GetPropertyValue("grouping"))
          );
    
  • Frans de Jong 550 posts 1862 karma points MVP 4x c-trib
    Dec 07, 2018 @ 09:16
    Frans de Jong
    100

    The error gave it away. You where execting IPublishedContent where you were getting IGrouping

    I always break up complex queries like this one in variables. That way you get a error in visual studio because the type doesn't match.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies