【问题标题】:grouping data from multiple tables using linq使用 linq 对来自多个表的数据进行分组
【发布时间】:2016-05-27 09:03:30
【问题描述】:

我正在加入几个表并尝试获取每个状态的计数。

EmployeeEvaluationsStatuses 具有以下字段:

ID、标题。

EmployeeEvaluationsStatuses 中的数据如下所示:

Id  Title
1   New - Incomplete
2   Submitted – All Docs
3   Approved
4   Rejected - Need More Info
5   Qualified

我想得到如下结果:

StatusCount  Status

60           New - Incomplete 
42           Submitted – All Docs 
20           Qualified 

这是我的查询的样子:

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”

我不确定导航属性。

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

【问题讨论】:

  • 您的实体之间是否有导航属性?你能展示你的实体类吗?
  • 我添加了实体框架下的实体类。
  • 你似乎有导航属性。你能显示EmployeePositions和EmployeeEvaluations吗?
  • 导航属性允许您以更容易和可读的方式编写查询。

标签: c# linq entity-framework-4


【解决方案1】:

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

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多