var queryCount = (from pv in db.Province
join pc in (from cc in
((from v in db.ERPStockProductTrigger
join s in db.Storage on v.StorageID equals s.StorageID
join b in db.BranchSet on s.BranchSetID equals b.Id
join p in db.Product on v.ProductID equals p.Id
join c in db.PriceProfile.Where(w => w.Type == 1) on p.Id equals c.ProductID
where s.RecordStatus == 1 && s.IsDelivery == true && s.IsWholesale == false
&& b.BranchType == 53 && b.RecordStatus == 1
&& p.RecordStatus == 1 && p.ProductName.Contains(pager.ProductName) && p.SkuCode.StartsWith(pager.SkuCode)
select new { p.SkuCode, s.ProvinceCode }).Distinct())
group cc by cc.ProvinceCode into gc
select new { ProvinceCode = gc.Key, Pcount = gc.Count() }) on pv.Code equals pc.ProvinceCode into tpc
from ttpc in tpc.DefaultIfEmpty()
join ps in (from cc in (from r in db.SupermarketProducts
join p in db.Product on r.ProductId equals p.Id
join c in db.PriceProfile.Where(w => w.Type == 1) on p.Id equals c.ProductID
where r.Status == 0 && r.IsEnable == 1 && p.ProductName.Contains(pager.ProductName) && p.SkuCode.StartsWith(pager.SkuCode)
select new { r.SkuCode, ProvinceCode = r.Province })
group cc by cc.ProvinceCode into gc
select new { ProvinceCode = gc.Key, Pcount = gc.Count() }) on pv.Code equals ps.ProvinceCode into tps
from ttps in tps.DefaultIfEmpty()
join pt in (from hr in db.SupermarketProductHis1
join br in (from cc in db.SupermarketProductHis1
group cc by cc.Province into gc
select new { Province = gc.Key, ReviseTime = gc.Max(cc => cc.ReviseTime) }
) on new { hr.Province, hr.ReviseTime } equals new { br.Province, br.ReviseTime }
select new { hr.ReviseTime, hr.Reviser, ProvinceCode = hr.Province }) on pv.Code equals pt.ProvinceCode into tpt
from ttpt in tpt.DefaultIfEmpty()
select new { Province = pv.Code, ProvinceName = pv.Name, SkuSum = (ttpc == null ? 0 : ttpc.Pcount), SkuSumSetting = (ttps == null ? 0 : ttps.Pcount), ttpt.ReviseTime, ttpt.Reviser }
);
queryCount = queryCount.Where(q => q.SkuSum > 0);
if (!string.IsNullOrWhiteSpace(pager.ProvinceCode))
{
queryCount = queryCount.Where(q => q.Province == pager.ProvinceCode);
}
data.total = queryCount.Count();
data.data = queryCount.Select(s => new SupermarketProductSum { Province = s.Province, ProvinceName = s.ProvinceName, SkuSum = s.SkuSum, SkuSumSetting = s.SkuSumSetting, ReviseTime = s.ReviseTime, Reviser = s.Reviser }).OrderBy(s => s.ProvinceName).ToList();