ASP.NET 将多个datatable整合到一个List中,DataTable.Select()过滤,获取最近12个月数据

private List<LabourTrendItem> GetLabourTrends(string projectId)
        {
            #region
            string sql = string.Empty;
            string sqlIn = string.Empty;
            string sqlOut = string.Empty;
            string sqlZC = string.Empty;
            //获取最近12个月的月份
            var beginMonth = DateTime.Now.AddMonths(-11).ToString("yyyy-MM");
            var endMonth = DateTime.Now.AddMonths(1).ToString("yyyy-MM");

            DateTime varDate = DateTime.Now.AddMonths(2);
            string curMonth = string.Empty;
            string curMonthJT = string.Empty;
            for (int i = 1; i < 13; i++)//遍历12次
            {
                curMonth = varDate.AddMonths(-i).ToString("yyyy-MM") + "-01";
                int j = i+1;
                curMonthJT = varDate.AddMonths(-j).ToString("yyyy-MM");

                #region 累计在场
                sqlZC += @" select IFNULL(sum(t2.ZcNum),0) as ZcNum, DATE_FORMAT('" + curMonth + @"','%Y-%m-%d') as curMonth
                                from
                                (
		                                select t1.identity_card,case when sum(t1.InNum)-sum(t1.OutNum) >0 then 1 else 0 end ZcNum
		                                from 
				                                (
					                                select b.identity_card,0 resumetype,count(r.id) InNum, 0 OutNum
					                                from hr_staff_pro_resume r 
					                                LEFT JOIN hr_staff_base b on r.base_id = b.id
                                                    LEFT JOIN project_info p on r.project_id = p.id and p.id= '" + projectId + @"'
					                                where r.resume_type = '已进场'
					                                AND r.in_date < DATE_FORMAT('" + curMonth + @"','%Y-%m-%d')
					                                GROUP BY b.identity_card
					                                union all
					                                select b.identity_card,1 resumetype,0 InNum,count(r.id) OutNum
					                                from hr_staff_pro_resume r 
					                                LEFT JOIN hr_staff_base b on r.base_id = b.id
                                                    LEFT JOIN project_info p on r.project_id = p.id and p.id= '" + projectId + @"'
					                                where r.resume_type = '已退场'
					                                AND r.out_date < DATE_FORMAT('" + curMonth + @"','%Y-%m-%d')
					                                GROUP BY b.identity_card
				                                ) t1
		                                group by t1.identity_card
                                ) t2 ";//累计在场
                #endregion

                #region 进场
                sqlIn += @" SELECT '" + curMonthJT + @"' AS curMonth, COUNT(*)  AS InNum
                                    FROM hr_staff_pro_resume t
                                    WHERE t.project_id = '" + projectId + @"'
                                    AND t.base_id NOT IN (SELECT id FROM hr_staff_base WHERE isblacklist in (0,2))
                                    AND t.subpack_id IN (SELECT id FROM subcontractor_base WHERE isblacklist is null or isblacklist = 0)
                                    AND t.in_date IS NOT NULL
                                    AND DATE_FORMAT(t.in_date,'%Y-%m') = '" + curMonthJT + @"' ";//进场
                #endregion

                #region 退场
                sqlOut += @" SELECT '" + curMonthJT + @"' AS curMonth, COUNT(*) AS OutNum
                                    FROM hr_staff_pro_resume t
                                    WHERE t.project_id = '" + projectId + @"'
                                    AND t.base_id NOT IN (SELECT id FROM hr_staff_base WHERE isblacklist in (0,2))
                                    AND t.subpack_id IN (
                                    SELECT id FROM subcontractor_base WHERE isblacklist is null or isblacklist = 0)
                                    AND t.out_date IS NOT NULL
                                    AND DATE_FORMAT(t.out_date,'%Y-%m') = '" + curMonthJT + @"' ";//退场
                #endregion

                if (i < 12)
                {
                    sqlZC += @" 
                                    UNION ALL  
                                ";

                    sqlIn += @" 
                                    UNION ALL  
                                ";

                    sqlOut += @" 
                                    UNION ALL  
                                ";
                }
            }
            DataTable dtZC = Context.FromSql(sqlZC).ToDataTable();
            DataTable dtIn = Context.FromSql(sqlIn).ToDataTable();
            DataTable dtOut = Context.FromSql(sqlOut).ToDataTable();


            List<LabourTrendItem> labourTrendList = new List<LabourTrendItem>();

            for (int i = 1; i < 13; i++)//遍历12次
            {
                curMonth = varDate.AddMonths(-i).ToString("yyyy-MM") + "-01";
                int j = i + 1;
                curMonthJT = varDate.AddMonths(-j).ToString("yyyy-MM");

                LabourTrendItem item = new LabourTrendItem();
                if (dtZC != null && dtZC.Rows.Count > 0)
                {
                    DataRow[] dr = dtZC.Select("curMonth ='" + curMonth + "'");
                    foreach (DataRow row in dr)
                    {
                        item.Month = Convert.ToDateTime(row["curMonth"]);
                        item.AvailableCount = row["ZcNum"].ToString();//在场
                    }
                }
                if (dtIn != null && dtIn.Rows.Count > 0)
                {
                    DataRow[] dr = dtIn.Select("curMonth ='"+ curMonthJT + "'");
                    foreach (DataRow row in dr)
                    {
                        item.Month = Convert.ToDateTime(row["curMonth"].ToString());
                        item.ComeCount = row["InNum"].ToString();//进场
                    }
                }
                if (dtOut != null && dtOut.Rows.Count > 0)
                {
                    DataRow[] dr = dtOut.Select("curMonth ='" + curMonthJT + "'");
                    foreach (DataRow row in dr)
                    {
                        item.Month = Convert.ToDateTime(row["curMonth"].ToString());
                        item.OutCount = row["OutNum"].ToString();//退场
                    }
                }
                labourTrendList.Add(item);
            }
            return labourTrendList.OrderBy(t => t.Month).ToList();
            #endregion
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值