项目linq group按结果分成单个视图模型对象?

时间:2014-05-16 12:43:32

标签: c# linq entity-framework

我想得到一些实体表的计数,并将它们分配给一个保存计数值的对象。

我正在使用union,因为我想对数据库执行单个查询。

我编写了以下代码,但这将为每个组返回一个单独的计数视图模型,而不是像将值分配给单个计数视图模型的属性。

var counts =
                _db.Departments.All()
                    .Select(c => new {key = 1, count = 0})
                    .Union(_db.Students.All().Select(c => new {key = 2, count= 0}))
                    .GroupBy(c=>c.key)                  
                    .Select(x => new CountsVm()
                    {
                        DepartmentCount = x.Count(d => d.key == 1),
                        StudentCount = x.Count(s => s.key == 2)
                    });



public class CountsVm
    {
        public int StudentCount { get; set; }
        public int DepartmentCount { get; set; }
    }

4 个答案:

答案 0 :(得分:1)

这是一个可以产生一个查询的解决方案

var countsQuery =
    _db.Departments.All()
        .Select(p => new { key = 1, count = 0 })
        .Union(_db.Students.All().Select(p => new { key = 2, count = 0 }))
        .GroupBy(p => p.key)
        .Select(p => new { key = p.Key, count = p.Count() }).ToList();
var counts = new CountsVm()
    {
        DepartmentCount =
            countsQuery.Where(p => p.key == 1)
                       .Select(p => p.count)
                       .FirstOrDefault(),
        StudentCount =
            countsQuery.Where(p => p.key == 2)
                       .Select(p => p.count)
                       .FirstOrDefault()
    };

答案 1 :(得分:0)

您是否只需要分别对每个条目表进行计数?

var counts = new CountsVm()
    {
        DepartmentCount = _db.Departments.All().Count(),
        StudentCount = _db.Students.All().Count()
    };

答案 2 :(得分:0)

如果我理解正确,你可以这样做:(我只使用linq完成,但在select中返回null并不是一个好习惯)。 foreach会更好地为你服务)

var countsVm = new CountsVm(){
     DepartmentCount = 0,
     StudentCount = 0
};

var counts =
                _db.Departments.All()
                    .Select(c => new {key = 1, count = 0})
                    .Union(_db.Students.All().Select(c => new {key = 2, count= 0}))
                    .GroupBy(c=>c.key)                  
                    .Select(x => {
                          countsVm.DepartmentCount += x.Count(d => d.key == 1);
                          countsVm.StudentCount += x.Count(s => s.key == 2);
                          return null;
                     });



public class CountsVm
    {
        public int StudentCount { get; set; }
        public int DepartmentCount { get; set; }
    }

答案 3 :(得分:0)

尝试从查询中删除All并运行FirstOrDefault()

var counts =
                _db.Departments.
                    .Select(c => new {key = 1, count = 0})
                    .Union(_db.Students.Select(c => new {key = 2, count= 0}))
                    .GroupBy(c=>c.key)                  
                    .Select(x => new CountsVm()
                    {
                        DepartmentCount = x.Count(d => d.key == 1),
                        StudentCount = x.Count(s => s.key == 2)
                    }).FirstOrDefault();



public class CountsVm
    {
        public int StudentCount { get; set; }
        public int DepartmentCount { get; set; }
    }