Results 1 to 6 of 6

Thread: Need help with LINQ Query - Order by bool, then parent -> children

  1. #1

    Thread Starter
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Need help with LINQ Query - Order by bool, then parent -> children

    Hi,
    I'm showing a list of services which have the following members (removed non-relevant ones):
    Code:
            public string Name { get; set; }
            public bool Compulsory { get; set; }
            public IList<Service> ChildServices { get; set; }
            public Service ParentService { get; set; }
    I want order showing the Compulsory one's first (though not so important), then showing each that have sub-services, then it's sub-services...make any sense?
    Like
    Service 55 (Compulsory)
    Service 2
    Service 2A
    Service 2B
    Service 3
    Service 3B
    ..etc

    At present I'm just ordering by compulsory and then name
    Code:
    model = _repository.GetAll().OrderByDescending(x => x.Compulsory).ThenBy(x=>x.Name);


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Need help with LINQ Query - Order by bool, then parent -> children

    This is untested but something like this should work:
    Code:
    model = _repository.GetAll()
                       .OrderByDescending(x => x.ParentService == null ? x.Compulsory : x.ParentService.Compulsory)
                       .ThenBy(x => x.ParentService == null ? x.Name : x.ParentName)
                       .ThenBy(x => x.ParentService == null ? x.Name : string.Empty);
    That assumes that no service is both a parent and a child.

  3. #3

    Thread Starter
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Re: Need help with LINQ Query - Order by bool, then parent -> children

    wow...would need to study a whole book to figure that one out. Thanks.

    Not totally right though. Should have used demo names so I could post the actual data, but what I have now:
    * I have 1 compulsory service, listed first. (nice)
    * Then I have a service with no parent or children, starting an A
    * Then I have 2 services, both starting with an H and sharing a parent starting with C
    * Then I have the parent service (starting with a C)
    * Then I have all my other services, which like the one starting with A, got no parent or children. They are listed alphabetically (nice)

    You are right. No service can be both parent and child. This is one level deep only


  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Need help with LINQ Query - Order by bool, then parent -> children

    So basically you're saying that a parent is listed after its children rather than before, correct? If so then try this:
    Code:
    model = _repository.GetAll()
                       .OrderByDescending(x => x.ParentService == null ? x.Compulsory : x.ParentService.Compulsory)
                       .ThenBy(x => x.ParentService == null ? x.Name : x.ParentName)
                       .ThenByDescending(x => x.ParentService == null)
                       .ThenBy(x => x.Name);
    That is, again, untested.

  5. #5

    Thread Starter
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Re: Need help with LINQ Query - Order by bool, then parent -> children

    Will see if I can figure it out, but right now get the following exception:
    Exception of type 'Antlr.Runtime.MismatchedTreeNodeException' was thrown.
    Exception of type 'Antlr.Runtime.MismatchedTreeNodeException' was thrown. [.ThenBy[MyApp.Domain.Entities.Service,System.String](.ThenByDescending[MyApp.Domain.EntitiesService,System.Boolean](.ThenBy[MyApp.Domain.Entities.Service,System.String](.OrderByDescending[MyApp.Domain.Entities.Service,System.Boolean](NHibernate.Linq.NhQueryable`1[MyApp.Domain.Entities.Service], Quote((x, ) => (Equal(x.ParentService, NULL) ? x.Compulsory : x.ParentService.Compulsory)), ), Quote((x, ) => (Equal(x.ParentService, NULL) ? x.Name : x.ParentService.Name)), ), Quote((x, ) => (Equal(x.ParentService, NULL))), ), Quote((x, ) => (x.Name)), )]


  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Need help with LINQ Query - Order by bool, then parent -> children

    Hmmm... I'm not sure where I got ParentName from but that should have been:
    Code:
    model = _repository.GetAll()
                       .OrderByDescending(x => x.ParentService == null ? x.Compulsory : x.ParentService.Compulsory)
                       .ThenBy(x => x.ParentService == null ? x.Name : x.ParentService.Name)
                       .ThenByDescending(x => x.ParentService == null)
                       .ThenBy(x => x.Name);
    The first call orders the compulsory services first, with children order by their parent's property rather than there own. This will put the compulsory service(s) before the non-compulsory one(s).

    The second call takes all those that are equivalent based on the first condition and sorts them by name, with children again ordered by their parent's property rather than their own. This will group each parent with its children with all the parents being in alphabetical order.

    The third call takes all those that are equivalent based on the first two conditions and sorts them on their whether they have a parent or not, with those that do not coming first. This will place each parent before its children.

    The fourth call takes all those that are equivalent based on the first three conditions and sorts them in alphabetical order by name. This will sort all siblings alphabetically.

    That's the theory of it all anyway. As I said, it's untested. If you do have issues then I'd suggest building it up one call at a time until you get a problem and then you know what level it's at.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width