码迷,mamicode.com
首页 > 其他好文 > 详细

利用EF Core的Lambda表达式和Join进行多表查询

时间:2020-05-12 11:54:08      阅读:351      评论:0      收藏:0      [点我收藏+]

标签:string   derby   where   rtt   null   color   count()   表达   sea   

直接上干货,利用EF Core的Lambda表达式和Join进行多表联动查询的实战应用

        #region 获取各个合作银行的意向单中金融分期申请情况
        /// <summary>
        /// 获取各个合作银行的意向单中金融分期申请情况
        /// </summary>
        /// <returns></returns>
        public List<ReadyApplyStatisticsViewModel> GetReadyApplyStatistics(ReadyApplyStatisticsViewModel_Search searchmodel)
        {
            List<ReadyApplyStatisticsViewModel> list = new List<ReadyApplyStatisticsViewModel>();
            var banklist = db.N_LoanBank.Where(a => a.Sort != 0).OrderBy(a => a.Sort).ToList();
            foreach (var item in banklist)
            {
                ReadyApplyStatisticsViewModel model = new ReadyApplyStatisticsViewModel();
                model.BankName = item.BankName;
                var ReadyApplyQuery = db.N_LoanReadyApply.Where(a => a.BankId == item.BankId && a.CreateDate > searchmodel.ReadyApplyStartTime && a.CreateDate < searchmodel.ReadyApplyEndTime).AsQueryable();
                var ApplyQuery = db.N_LoanApply.Where(b => b.BankId == item.BankId && b.ApplyTime>searchmodel.ApplyStartTime && b.ApplyTime<searchmodel.ApplyEndTime).AsQueryable();
                if(searchmodel.BusinessType!=10)
                {
                    ApplyQuery = ApplyQuery.Where(b=>b.BusinessType==searchmodel.BusinessType);
                }
                if(!string.IsNullOrEmpty(searchmodel.ApplyChannel))
                {
                    ApplyQuery = ApplyQuery.Where(b=>b.ApplyChannel.Contains(searchmodel.ApplyChannel));
                }
                model.ReadyApplyCount = ReadyApplyQuery.Count();
                model.ApplyCount = ReadyApplyQuery.Join(ApplyQuery, a => a.BidderNo, b => b.BidderNo, (a, b) => new { BidderNo = a.BidderNo }).Count();
                model.WaitAcceptCount = ReadyApplyQuery.Join(ApplyQuery.Where(b=>b.AcceptStatus==0 && b.CheckStatus == 0 && DateTime.Now <= b.AcceptExpiryTime), a => a.BidderNo, b => b.BidderNo, (a, b) => new { BidderNo = a.BidderNo }).Count();
                model.AcceptCount = ReadyApplyQuery.Join(ApplyQuery.Where(b => b.AcceptStatus == 0 && b.CheckStatus == 1), a => a.BidderNo, b => b.BidderNo, (a, b) => new { BidderNo = a.BidderNo }).Count();
                model.NotAcceptCount = ReadyApplyQuery.Join(ApplyQuery.Where(b => b.AcceptStatus == 2), a => a.BidderNo, b => b.BidderNo, (a, b) => new { BidderNo = a.BidderNo }).Count();
                model.CheckCount = ReadyApplyQuery.Join(ApplyQuery.Where(b => b.AcceptStatus == 1 && b.CheckStatus==1), a => a.BidderNo, b => b.BidderNo, (a, b) => new { BidderNo = a.BidderNo }).Count();
                model.NotCheckCount = ReadyApplyQuery.Join(ApplyQuery.Where(b => b.AcceptStatus == 1 && b.CheckStatus == 2), a => a.BidderNo, b => b.BidderNo, (a, b) => new { BidderNo = a.BidderNo }).Count();
                model.CancelCheckCount = ReadyApplyQuery.Join(ApplyQuery.Where(b => b.AcceptStatus == 1 && b.CheckStatus == 3), a => a.BidderNo, b => b.BidderNo, (a, b) => new { BidderNo = a.BidderNo }).Count();
                model.MoneyCheckCount = ReadyApplyQuery.Join(ApplyQuery.Where(b => b.AcceptStatus == 1 && b.CheckStatus == 1 && b.PaymentStatus==1 && b.MoneyCheckStatus==1), a => a.BidderNo, b => b.BidderNo, (a, b) => new { BidderNo = a.BidderNo }).Count();
                model.NotMoneyCheckCount = ReadyApplyQuery.Join(ApplyQuery.Where(b => b.AcceptStatus == 1 && b.CheckStatus == 1 && b.PaymentStatus == 1 && b.MoneyCheckStatus == 0), a => a.BidderNo, b => b.BidderNo, (a, b) => new { BidderNo = a.BidderNo }).Count();
                list.Add(model);
            }
            ReadyApplyStatisticsViewModel summodel = new ReadyApplyStatisticsViewModel();
            summodel.BankName = "总计";
            summodel.ReadyApplyCount = list.Sum(a => a.ReadyApplyCount);
            summodel.ApplyCount = list.Sum(a => a.ApplyCount);
            summodel.WaitAcceptCount = list.Sum(a => a.WaitAcceptCount);
            summodel.AcceptCount = list.Sum(a => a.AcceptCount);
            summodel.NotAcceptCount = list.Sum(a => a.NotAcceptCount);
            summodel.CheckCount = list.Sum(a => a.CheckCount);
            summodel.NotCheckCount = list.Sum(a => a.NotCheckCount);
            summodel.CancelCheckCount = list.Sum(a => a.CancelCheckCount);
            summodel.MoneyCheckCount = list.Sum(a => a.MoneyCheckCount);
            summodel.NotMoneyCheckCount = list.Sum(a => a.NotMoneyCheckCount);
            list.Add(summodel);
            return list;
        }
        #endregion

 

利用EF Core的Lambda表达式和Join进行多表查询

标签:string   derby   where   rtt   null   color   count()   表达   sea   

原文地址:https://www.cnblogs.com/yechangzhong-826217795/p/12874813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!