首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用linq对多个表中的数据进行分组

使用linq对多个表中的数据进行分组
EN

Stack Overflow用户
提问于 2016-05-27 07:08:46
回答 1查看 42关注 0票数 0

我正在加入几个表,并试图获得每个状态的计数。

EmployeeEvaluationsStatuses包含以下字段:

身份,头衔。

EmployeeEvaluationsStatuses中的数据如下所示:

代码语言:javascript
复制
Id  Title
1   New - Incomplete
2   Submitted – All Docs
3   Approved
4   Rejected - Need More Info
5   Qualified

我想得到如下结果:

代码语言:javascript
复制
StatusCount  Status

60           New - Incomplete 
42           Submitted – All Docs 
20           Qualified 

下面是我的查询:

代码语言:javascript
复制
from ep in EmployeePositions.Where(a => a.CorporateId == 1596)
join ee in EmployeeEvaluations.Where(e => e.TargetGroupId != null) on ep.EmployeeId equals ee.EmployeeId
join ees in EmployeeEvaluationStatuses  on ee.EvaluationStatusId  equals ees.Id

group ees by ees.Id into g
select new
{
   StatusCount = g.Count()
   ,Status= ees .title      
}

我收到一个错误"The name 'ees' does not exist in the current context

我不确定导航属性。

代码语言:javascript
复制
 public partial class WotcEntities : DbContext
    {
        public WotcEntities()
       : base(hr.common.Database.EntitiesConnectionString("res://*/ef.WotcModel.csdl|res://*/ef.WotcModel.ssdl|res://*/ef.WotcModel.msl", "devConnection"))
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }


      public virtual DbSet<EmployeeEvaluations> EmployeeEvaluations { get; set; }
      public virtual DbSet<EmployeeEvaluationStatus> EmployeeEvaluationStatus { get; set; }
        public virtual DbSet<EmployeePositions> EmployeePositions { get; set; }

    }




public partial class EmployeeEvaluationStatus
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public EmployeeEvaluationStatus()
        {
            this.EmployeeEvaluations = new HashSet<EmployeeEvaluations>();
            this.Vouchers = new HashSet<Vouchers>();
        }

        public int Id { get; set; }
        public string Title { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<EmployeeEvaluations> EmployeeEvaluations { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Vouchers> Vouchers { get; set; }
    }
EN

回答 1

Stack Overflow用户

发布于 2016-05-27 09:03:31

您必须在group by中使用组合键并使用g.Key进行导航

代码语言:javascript
复制
from ep in EmployeePositions.Where(a => a.CorporateId == 1596)
join ee in EmployeeEvaluations.Where(e => e.TargetGroupId != null) on ep.EmployeeId equals ee.EmployeeId
join ees in EmployeeEvaluationStatuses on ee.EvaluationStatusId  equals ees.Id

group ees by new { ees.Id, ees.title } into g
select new
{
    StatusCount = g.Count(),
    Status= g.Key.title
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37472445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档