简体   繁体   中英

grouping data from multiple tables using linq

I am joining couple tables and trying to get the count of each status.

EmployeeEvaluationsStatuses has the following fields :

Id, Title.

Data in EmployeeEvaluationsStatuses look like :

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

I want to get the result as follows :

StatusCount  Status

60           New - Incomplete 
42           Submitted – All Docs 
20           Qualified 

Here is how my query look like :

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      
}

I am getting an error " The name 'ees' does not exist in the current context "

I am not sure about navigational properties.

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

You have to use a composite key in your group by and navigate using 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM