tobias
tobias

Reputation: 1590

Linq Query with using sum and order by

i have class like below,

public class SiteActionMap
{
        public int Id { get; set; }
        public string ImageName { get; set; }
        public int ParentId { get; set; }
        public string Name { get; set; }

        public  virtual ICollection<SiteActionMap> Childrens { get; set; }
        public  virtual SiteActionMap Parent { get; set; }
        public bool Active { get; set; }

}

and i want to get a list with order by Children counts.

i am trying something like this,but i didnt work.

List<SiteActionMap> list = dbContext.SiteActionMap
                .Include(m => m.Childrens)
                .Include(m => m.Parent)
                .OrderBy(i => i.Childrens.Count)
                ToList();

how i can sort my list with children counts?

Upvotes: 1

Views: 922

Answers (2)

naspinski
naspinski

Reputation: 34689

Adding on to what lbruder said, isn't Count a method () in this case?

List<SiteActionMap> list = dbContext.SiteActionMap
                .Include(m => m.Childrens)
                .Include(m => m.Parent)
                .OrderByDescending(i => i.Childrens.Count())
                .ToList();

Upvotes: 4

user434817
user434817

Reputation:

Have you tried this? Just remove the semicolon after the Order... line and continue the command:

List<SiteActionMap> list = dbContext.SiteActionMap
                .Include(m => m.Childrens)
                .Include(m => m.Parent)
                .OrderByDescending(i => i.Childrens.Count)
                .ToList();

Upvotes: 2

Related Questions