Linq查询使用sum和order by

时间:2012-05-02 18:30:37

标签: c# .net linq entity-framework entity-framework-4

我的课程如下,

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; }

}

我希望得到一份按孩子计算的订单列表。

我正在尝试这样的事情,但我没有工作。

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

我如何根据孩子的数量对我的名单进行排序?

2 个答案:

答案 0 :(得分:4)

添加lbruder所说的,在这种情况下不计算一个方法()吗?

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

答案 1 :(得分:2)

你试过这个吗?只需在Order ...行后删除分号,然后继续执行命令:

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