Linq Group by

 [Table("Emp_EmployeeSocialFundlInfo")]
    public class EmployeeSocialFundlInfoEntity : BaseCreateUpdateLogEntity<Guid>
    {
        #region Properties
      
        /// <summary>
        /// 机构ID
        /// </summary>
        public Guid InstitutionID { get; set; }

        /// <summary>
        /// 员工ID
        /// </summary>
        public Guid EmployeeID { get; set; }

        /// <summary>
        /// 证件号码
        /// </summary>
        public string Credential { get; set; }

        /// <summary>
        /// 服务事件代码
        /// </summary>
        public string ServiceCode { get; set; }

        /// <summary>
        /// 福利地
        /// </summary>
        public Guid? WelfareID { get; set; }
        public string WelfareName { get; set; }
        /// <summary>
        /// 方案
        /// </summary>
        public Guid? SolutionID { get; set; }
        public string SolutionName { get; set; }
        /// <summary>
        /// 实际参保性质
        /// </summary>
        public Guid? ActualHouseholdTypeID { get; set; }
        public string ActualHouseholdTypeText { get; set; }

        /// <summary>
        /// 以下是新增或补缴字段
        /// </summary>
        public Guid? TaskPackageID { get; set; }
        public Guid? TaskID { get; set; }
        public DateTime? ActualInsuranceDate { get; set; }
        public decimal? ActualInsuranceBase { get; set; }
        public int? TaskStatus { get; set; }
        public DateTime? TaskCreateOn { get; set; }

        public string ActualCustomerNo { get; set; }
        public decimal? DeclareEnterprisePercent { get; set; }
        public decimal? DeclarePersonalPercent { get; set; }
        public decimal? ActualAmount { get; set; }

        /// <summary>
        /// 以下是终止字段
        /// </summary>
        public Guid? EndTaskID { get; set; }
        public string EndActualCustomerNo { get; set; }
        public DateTime? EndActualInsuranceDate { get; set; }
        public decimal? EndActualInsuranceBase { get; set; }
        public int? EndTaskStatus { get; set; }

        public bool IsLatest { get; set; }
        
        #endregion
    }
View Code
            // 先获取同一个员工下的社保,再统计福利地分布情况
            var sums = employeeSocialFundlInfo.Where<EmployeeSocialFundlInfoEntity>(p => p.ServiceCode != "FundInsuranceNew")
                                                        .GroupBy(x => new { x.EmployeeID, x.WelfareName }).Select(group => new { Peo = group.Key, Count = group.Count() })
                                                        .GroupBy(x => x.Peo.WelfareName).Select(group => new { WelfareName = group.Key, Count = group.Count() });
            foreach (var employee in sums)
            {
                result[employee.WelfareName] = employee.Count;
            }

关于key的问题

    public class Personal
    {
        public Guid ID { get; set; }
        /// <summary>
        /// 15位证件号
        /// </summary>
        public string Name { get; set; }
        public int Age { get; set; }
        /// <summary>
        /// 服务费
        /// </summary>
        public decimal ServiceCharge { get; set; }
    }
View Code
            var list = new List<Personal>();
            list.Add(new Personal
            {
                ID = Guid.NewGuid(),
                Name = "张三",
                Age = 11,
                ServiceCharge = 11M,

            });
            list.Add(new Personal
            {
                ID = Guid.NewGuid(),
                Name = "张三",
                Age = 11,
                ServiceCharge = 12M,
            });
            list.Add(new Personal
            {
                ID = Guid.NewGuid(),
                Name = "李四",
                Age = 34,
                ServiceCharge = 13M,
            });
            list = list.GroupBy(x => new { x.Name, x.Age }).Select(g => new Personal() { Name = g.Key.Name, Age = g.Key.Age, ServiceCharge = g.Max(p => p.ServiceCharge) }).ToList();
View Code

 

posted @ 2015-11-19 13:34  江境纣州  阅读(13)  评论(0编辑  收藏  举报