首页 > 代码库 > 一句话,将Linq语句转换为Sql语句

一句话,将Linq语句转换为Sql语句



   public IEnumerable<VMB_Report> ReportView_List(VMB_ReportConditions requiredModel)
        {
          
            IEnumerable<VMB_Report> resultModel = new List<VMB_Report>();
         IQueryable<Merchant> merchantList;

              merchantList = Report_List();
          

            #region 从MerchantList中获取VMB_Report集合


            resultModel = merchantList.Select(o => new VMB_Report
            {

                MerchantID = o.MerchantID,
                MID = o.MID,
                DBA = o.DBA,
                DBAPhone = o.DBAPhone,
                ContactName = o.ContactName,
                TotalSalesYTD = o.MerchantVolume.TotalSalesYTD,
                BankcardAvgTicketYTD = o.MerchantVolume.BankcardAvgTicketYTD,
                YTDProfit = o.MerchantVolume.YTDProfit,
                EnteredDate = o.MerchantStatus.EnteredDate,
                InstalledDate = o.MerchantStatus.InstalledDate,
                Status = o.MerchantStatus.Status,
                RegionName = o.MerchantReps.Where(a => a.LevelNumber == 1).FirstOrDefault().Rep.Region.RegionName,
                RepLev1 = (o.MerchantReps.Any(a => a.LevelNumber == 1) ? o.MerchantReps.Where(a => a.LevelNumber == 1).FirstOrDefault().Rep.Name : ""),
                RepLev2 = (o.MerchantReps.Any(a => a.LevelNumber == 2) ? o.MerchantReps.Where(a => a.LevelNumber == 2).FirstOrDefault().Rep.Name : ""),
                RepLev3 = (o.MerchantReps.Any(a => a.LevelNumber == 3) ? o.MerchantReps.Where(a => a.LevelNumber == 3).FirstOrDefault().Rep.Name : ""),
                RepLev4 = (o.MerchantReps.Any(a => a.LevelNumber == 4) ? o.MerchantReps.Where(a => a.LevelNumber == 4).FirstOrDefault().Rep.Name : ""),
                RepLev5 = (o.MerchantReps.Any(a => a.LevelNumber == 5) ? o.MerchantReps.Where(a => a.LevelNumber == 5).FirstOrDefault().Rep.Name : ""),
                Address1 = o.DBAAddress1,
                Address2 = o.DBAAddress2,
                City = o.DBACity,
                State = o.DBAState,
                Zip = o.DBAZip,
                Ps = (from op in o.Products
                     where op.Status == 100
                     select new VMPro
                     {
                         EFDe = op.EquipmentFee.Description,
                         Ptype = op.ProductType,
                         SDe = op.Software.Description
                     }).ToList()

            });
            #endregion

            Var sql=(resultModel as ObjectQuery<VMB_Report>).ToTraceString();//此句为精华,查看sql变量就是生成的sql语句。

          return resultModel;
        }