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 11 posts 101 karma points
    4 days ago
    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 406 posts 1344 karma points c-trib
    4 days ago
    Frans de Jong
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft