C# 加速linq实体查询
我目前有一个查询,它不需要很长时间,有时会因为数据库中的数据量而崩溃 有人能注意到我能做些什么来加速它吗C# 加速linq实体查询,c#,.net,performance,entity-framework,linq,C#,.net,Performance,Entity Framework,Linq,我目前有一个查询,它不需要很长时间,有时会因为数据库中的数据量而崩溃 有人能注意到我能做些什么来加速它吗 public IList<Report> GetReport(CmsEntities context, long manufacturerId, long? regionId, long? vehicleTypeId) { var now = DateTime.Now; var today = new DateTim
public IList<Report> GetReport(CmsEntities context, long manufacturerId, long? regionId, long? vehicleTypeId)
{
var now = DateTime.Now;
var today = new DateTime(now.Year, now.Month, 1);
var date1monthago = today.AddMonths(-1);
var date2monthago = today.AddMonths(-2);
var date3monthago = today.AddMonths(-3);
var date4monthago = today.AddMonths(-4);
var date5monthago = today.AddMonths(-5);
var date6monthago = today.AddMonths(-6);
today = TimeManager.EndOfDay(new DateTime(now.AddMonths(-1).Year, today.AddMonths(-1).Month, DateTime.DaysInMonth(now.Year, today.AddMonths(-1).Month)));
var query = from item in context.Invoices
where item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Select(x => x.ManufacturerId).Contains(manufacturerId)
&& (item.InvoiceDate >= date6monthago && item.InvoiceDate <= today)
&& (regionId.HasValue && regionId.Value > 0 ? item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Select(x => x.RegionId).Contains(regionId.Value) : true)
&& (item.InvType == "I" || item.InvType == null)
&& (vehicleTypeId.HasValue && vehicleTypeId.Value > 0 ? item.Repair.Job.Vehicle.Model.VehicleTypes.Select(x => x.Id).Contains(vehicleTypeId.Value) : true)
select item;
var query2 = from item in query
group item by new { item.Repair.Job.Bodyshop } into g
let manufJobs = query.Where(x => x.Repair.Job.Vehicle.Model.ManufacturerId == manufacturerId && x.Repair.Job.BodyshopId == g.Key.Bodyshop.Id)
let allJobs = query.Where(x => x.Repair.Job.BodyshopId == g.Key.Bodyshop.Id)
select new tReport
{
MonthSixManufJobTotal = manufJobs.Where(x => x.InvoiceDate.Month == date6monthago.Month && x.InvoiceDate.Year == date6monthago.Year).GroupBy(x => x.Repair.Job).Count(),
MonthSixJobTotal = allJobs.Where(x => x.InvoiceDate.Month == date6monthago.Month && x.InvoiceDate.Year == date6monthago.Year).GroupBy(x => x.Repair.Job).Count(),
MonthFiveManufJobTotal = manufJobs.Where(x => x.InvoiceDate.Month == date5monthago.Month && x.InvoiceDate.Year == date5monthago.Year).GroupBy(x => x.Repair.Job).Count(),
MonthFiveJobTotal = allJobs.Where(x => x.InvoiceDate.Month == date5monthago.Month && x.InvoiceDate.Year == date5monthago.Year).GroupBy(x => x.Repair.Job).Count(),
MonthFourManufJobTotal = manufJobs.Where(x => x.InvoiceDate.Month == date4monthago.Month && x.InvoiceDate.Year == date4monthago.Year).GroupBy(x => x.Repair.Job).Count(),
MonthFourJobTotal = allJobs.Where(x => x.InvoiceDate.Month == date4monthago.Month && x.InvoiceDate.Year == date4monthago.Year).GroupBy(x => x.Repair.Job).Count(),
MonthThreeManufJobTotal = manufJobs.Where(x => x.InvoiceDate.Month == date3monthago.Month && x.InvoiceDate.Year == date3monthago.Year).GroupBy(x => x.Repair.Job).Count(),
MonthThreeJobTotal = allJobs.Where(x => x.InvoiceDate.Month == date3monthago.Month && x.InvoiceDate.Year == date3monthago.Year).GroupBy(x => x.Repair.Job).Count(),
MonthTwoManufJobTotal = manufJobs.Where(x => x.InvoiceDate.Month == date2monthago.Month && x.InvoiceDate.Year == date2monthago.Year).GroupBy(x => x.Repair.Job).Count(),
MonthTwoJobTotal = allJobs.Where(x => x.InvoiceDate.Month == date2monthago.Month && x.InvoiceDate.Year == date2monthago.Year).GroupBy(x => x.Repair.Job).Count(),
MonthOneManufJobTotal = manufJobs.Where(x => x.InvoiceDate.Month == date1monthago.Month && x.InvoiceDate.Year == date1monthago.Year).GroupBy(x => x.Repair.Job).Count(),
MonthOneJobTotal = allJobs.Where(x => x.InvoiceDate.Month == date1monthago.Month && x.InvoiceDate.Year == date1monthago.Year).GroupBy(x => x.Repair.Job).Count(),
ManufTotal = manufJobs.GroupBy(x => x.Repair.Job).Count(),
Total = allJobs.GroupBy(x => x.Repair.Job).Count(),
PercentageOf = ((decimal)manufJobs.GroupBy(x => x.Repair.Job).Count() / (decimal)allJobs.GroupBy(x => x.Repair.Job).Count()) * 100
};
return query2.OrderBy(x => x).ToList();
}
public IList GetReport(CMSenties上下文、long manufacturerId、long?regionId、long?vehicleTypeId)
{
var now=DateTime.now;
var today=新日期时间(现在.年,现在.月,1);
var date1monthago=今天.AddMonths(-1);
var Date2Monmonthago=今天.AddMonths(-2);
var Date3个月前=今天。添加月份(-3);
var date4monthago=今天.AddMonths(-4);
var Date5Monmonthago=今天.AddMonths(-5);
var date6monthago=今天.AddMonths(-6);
today=TimeManager.EndOfDay(新的日期时间(now.AddMonths(-1).Year,today.AddMonths(-1).Month,DateTime.DaysInMonth(now.Year,today.AddMonths(-1).Month);
var query=来自上下文中的项目。发票
其中item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Select(x=>x.ManufacturerId.Contains(ManufacturerId)
&&(item.InvoiceDate>=date6monthago&&item.InvoiceDate 0?item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Select(x=>x.RegionId.Contains(RegionId.Value):true)
&&(item.InvType==“I”| | item.InvType==null)
&&(vehicleTypeId.HasValue&&vehicleTypeId.Value>0?item.Repair.Job.Vehicle.Model.VehicleTypes.Select(x=>x.Id)。包含(vehicleTypeId.Value):true)
选择项目;
var query2=来自查询中的项
按新{item.Repair.Job.Bodyshop}将项目分组到g中
设manufJobs=query.Where(x=>x.Repair.Job.Vehicle.Model.ManufacturerId==ManufacturerId&&x.Repair.Job.BodyshopId==g.Key.Bodyshop.Id)
让allJobs=query.Where(x=>x.Repair.Job.BodyshopId==g.Key.Bodyshop.Id)
选择new tReport
{
MonthximanufjobTotal=manufJobs.Where(x=>x.InvoiceDate.Month==date6monthago.Month&&x.InvoiceDate.Year==date6monthago.Year)。GroupBy(x=>x.Repair.Job)。Count(),
MonthSixJobTotal=allJobs.Where(x=>x.InvoiceDate.Month==date6monthago.Month&&x.InvoiceDate.Year==date6monthago.Year).GroupBy(x=>x.Repair.Job).Count(),
MonthFiveManufJobTotal=manufJobs.Where(x=>x.InvoiceDate.Month==Date5Monmonthago.Month&&x.InvoiceDate.Year==Date5Monmonthago.Year)。GroupBy(x=>x.Repair.Job)。Count(),
MonthFiveJobTotal=allJobs.Where(x=>x.InvoiceDate.Month==Date5Monmonthago.Month&&x.InvoiceDate.Year==Date5Monmonthago.Year).GroupBy(x=>x.Repair.Job).Count(),
MonthFourManufJobTotal=manufJobs.Where(x=>x.InvoiceDate.Month==date4monthago.Month&&x.InvoiceDate.Year==date4monthago.Year)。GroupBy(x=>x.Repair.Job)。Count(),
MonthFourJobTotal=所有作业。其中(x=>x.InvoiceDate.Month==date4monthago.Month&&x.InvoiceDate.Year==date4monthago.Year)。GroupBy(x=>x.Repair.Job)。计数(),
MonthThreManufjobTotal=manufJobs.Where(x=>x.InvoiceDate.Month==date3monthago.Month&&x.InvoiceDate.Year==date3monthago.Year)。GroupBy(x=>x.Repair.Job)。Count(),
MonthThreJobTotal=allJobs.Where(x=>x.InvoiceDate.Month==date3monthago.Month&&x.InvoiceDate.Year==date3monthago.Year)。GroupBy(x=>x.Repair.Job)。Count(),
MonthWomanufjobTotal=manufJobs.Where(x=>x.InvoiceDate.Month==Date2monago.Month&&x.InvoiceDate.Year==Date2monago.Year)。GroupBy(x=>x.Repair.Job)。Count(),
MonthTwoJobTotal=allJobs.Where(x=>x.InvoiceDate.Month==Date2MonMonthAgo.Month&&x.InvoiceDate.Year==Date2MonMonthAgo.Year).GroupBy(x=>x.Repair.Job).Count(),
MonthOneManufJobTotal=manufJobs.Where(x=>x.InvoiceDate.Month==Date1Monmonago.Month&&x.InvoiceDate.Year==Date1Monmonago.Year)。GroupBy(x=>x.Repair.Job)。Count(),
MonthOneJobTotal=所有作业。其中(x=>x.InvoiceDate.Month==Date1Monago.Month&&x.InvoiceDate.Year==Date1Monago.Year)。分组依据(x=>x.Repair.Job)。计数(),
ManufTotal=manufJobs.GroupBy(x=>x.Repair.Job).Count(),
总计=所有作业.GroupBy(x=>x.Repair.Job).Count(),
PercentageOf=((十进制)manufJobs.GroupBy(x=>x.Repair.Job).Count()/(十进制)allJobs.GroupBy(x=>x.Repair.Job).Count())*100
};
返回query2.OrderBy(x=>x.ToList();
}
编辑
var query=来自context.Invoices.AsNoTracking()中的项目
其中item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Any(x=>x.ManufacturerId==ManufacturerId)
&&(item.InvoiceDate>=12个月前的日期和&item.InvoiceDate 0)
{
query=query.Where(item=>item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Select(x=>x.RegionId).Contains(RegionId.Value));
}
if(vehicleTypeId.HasValue&&vehicleTypeId.Value>0)
{
query=query.Where(item=>item.Repair.Job.Vehicle.Model.VehicleTypes.Select(x=>x.Id).Contains(vehicleTypeId.Value));
}
var query2=来自hey中的项目
按新{item.Repair.Job.Bodyshop,item.InvoiceDate.Month}将项目分组到m中
选择new TReport
{
车身车间=m.Key.Bodyshop.Name,
间隔=m.Key.Bodyshop.Bays,
Region=m.Key.Bodyshop.Manufacturer2Bodyshop.FirstOrDefault(x=>x.ManufacturerId==ManufacturerId).Region.Name,
BodyshopCode=m.Key.Bodyshop.Manufacturer2Bodyshop.FirstOrDefault(x=>x.ManufacturerId==ManufacturerId).BodyshopCode,
总计=m.计数(),
ManufTotal=m.Where(x=>x.Repair.Job.Vehicle.Model.ManufacturerId==ManufacturerId).Count(),
总计=m.GroupBy(j=>j.InvoiceDate.Month)。选择(
var query = from item in context.Invoices.AsNoTracking()
where item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Any(x => x.ManufacturerId == manufacturerId)
&& (item.InvoiceDate >= date12monthago && item.InvoiceDate <= today)
&& (item.InvType == "I" || item.InvType == null)
select item;
if (regionId.HasValue && regionId.Value > 0)
{
query = query.Where(item => item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Select(x => x.RegionId).Contains(regionId.Value));
}
if (vehicleTypeId.HasValue && vehicleTypeId.Value > 0)
{
query = query.Where(item => item.Repair.Job.Vehicle.Model.VehicleTypes.Select(x => x.Id).Contains(vehicleTypeId.Value));
}
var query2 = from item in hey
group item by new { item.Repair.Job.Bodyshop, item.InvoiceDate.Month } into m
select new TReport
{
Bodyshop = m.Key.Bodyshop.Name,
Bays = m.Key.Bodyshop.Bays,
Region = m.Key.Bodyshop.Manufacturer2Bodyshop.FirstOrDefault(x => x.ManufacturerId == manufacturerId).Region.Name,
BodyshopCode = m.Key.Bodyshop.Manufacturer2Bodyshop.FirstOrDefault(x => x.ManufacturerId == manufacturerId).BodyshopCode,
Total = m.Count(),
ManufTotal = m.Where(x => x.Repair.Job.Vehicle.Model.ManufacturerId == manufacturerId).Count(),
Totals = m.GroupBy(j => j.InvoiceDate.Month).Select(j => new TPercentReportInner
{
Month = j.Key,
ManufTotal = j.Where(x => x.Repair.Job.Vehicle.Model.ManufacturerId == manufacturerId).Count(),
AllTotal = j.Count()
})
};
public IList<Report> GetReport(CmsEntities context, long manufacturerId, long? regionId, long? vehicleTypeId, int pageSize, int currentPage)
{
//Code removed to simplify
return query2.Skip(pageSize * currentPage).Take(pageSize );
}
var allJobs = query.Where(x => x.Repair.Job.BodyshopId == g.Key.Bodyshop.Id).ToArray();
MonthSixManufJobTotal = manufJobs.Where(x => x.InvoiceDate.Month == date6monthago.Month && x.InvoiceDate.Year == date6monthago.Year).GroupBy(x => x.Repair.Job).Count(),
MonthSixJobTotal = allJobs.Where(x => x.InvoiceDate.Month == date6monthago.Month && x.InvoiceDate.Year == date6monthago.Year).GroupBy(x => x.Repair.Job).Count(),
var query = from item in context.Invoices.AsNoTracking()
where item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Select(x => x.ManufacturerId).Contains(manufacturerId)
&& (item.InvoiceDate >= date12monthago && item.InvoiceDate <= today)
&& (item.InvType == "I" || item.InvType == null)
select item;
if (regionId.HasValue && regionId.Value > 0)
query=query.Where(item=>item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Select(x => x.RegionId).Contains(regionId.Value));
if (vehicleTypeId.HasValue && vehicleTypeId.Value > 0)
query=query.Where(item=>item.Repair.Job.Vehicle.Model.VehicleTypes.Select(x => x.Id).Contains(vehicleTypeId.Value));
var query2 = from item in query
group item by new { item.InvoiceDate.Month, item.Repair.Job.Bodyshop } into g
select new TReport
{
BodyshopId = g.Key.Bodyshop.Id,
Month = g.Key.Month,
MonthAllJobTotal = g.Count()
};
return query2.ToList();
var query = from item in context.Invoices.AsNoTracking()
where item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Any(m=>m.ManufacturerId==manufacturerId)
&& (item.InvoiceDate >= date12monthago && item.InvoiceDate <= today)
&& (item.InvType == "I" || item.InvType == null)
select item;
if (regionId.HasValue && regionId.Value > 0)
query=query.Where(item=>item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Any(m=>m.RegionId==regionId.Value));
if (vehicleTypeId.HasValue && vehicleTypeId.Value > 0)
query=query.Where(item=>item.Repair.Job.Vehicle.Model.VehicleTypes.Any(v=>v.Id==vehicleTypeId.Value));
var query2 = from item in query
group item by new { item.InvoiceDate.Month, item.Repair.Job.Bodyshop } into g
select new TReport
{
BodyshopId = g.Key.Bodyshop.Id,
Month = g.Key.Month,
MonthAllJobTotal = g.Count()
};
return query2.ToList();
var query = from item in context.Invoices.AsNoTracking()
where (item.InvoiceDate >= date12monthago && item.InvoiceDate <= today)
&& (item.InvType == "I" || item.InvType == null)
select item;
if (manufacturerId.HasValue && manufacturerId.Value > 0)
query=query.Where(item=>item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Any(m=>m.ManufacturerId==manufacturerId));
if (regionId.HasValue && regionId.Value > 0)
query=query.Where(item=>item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Any(m=>m.RegionId==regionId.Value));
if (vehicleTypeId.HasValue && vehicleTypeId.Value > 0)
query=query.Where(item=>item.Repair.Job.Vehicle.Model.VehicleTypes.Any(v=>v.Id==vehicleTypeId.Value));
var query2 = from item in query
group item by new { item.InvoiceDate.Month, item.Repair.Job.Bodyshop } into g
select new TReport
{
BodyshopId = g.Key.Bodyshop.Id,
Month = g.Key.Month,
MonthAllJobTotal = g.Count()
};
return query2.ToList();
public IQueryable<Report> GetReport(CmsEntities context, long? manufacturerId, long? regionId, long? vehicleTypeId)
{
{
var now = DateTime.Now;
var today = new DateTime(now.Year, now.Month, 1);
var date1monthago = today.AddMonths(-1);
var date2monthago = today.AddMonths(-2);
var date3monthago = today.AddMonths(-3);
var date4monthago = today.AddMonths(-4);
var date5monthago = today.AddMonths(-5);
var date6monthago = today.AddMonths(-6);
today = TimeManager.EndOfDay(new DateTime(now.AddMonths(-1).Year, today.AddMonths(-1).Month, DateTime.DaysInMonth(now.Year, today.AddMonths(-1).Month)));
var query = from item in context.Invoices.AsNoTracking()
where (item.InvoiceDate >= date12monthago && item.InvoiceDate <= today)
&& (item.InvType == "I" || item.InvType == null)
select item;
if (manufacturerId.HasValue && manufacturerId.Value > 0)
query=query.Where(item=>item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Any(m=>m.ManufacturerId==manufacturerId));
if (regionId.HasValue && regionId.Value > 0)
query=query.Where(item=>item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Any(m=>m.RegionId==regionId.Value));
if (vehicleTypeId.HasValue && vehicleTypeId.Value > 0)
query=query.Where(item=>item.Repair.Job.Vehicle.Model.VehicleTypes.Any(v=>v.Id==vehicleTypeId.Value));
var query2 = from item in query
group item by new { item.InvoiceDate.Month, item.Repair.Job.Bodyshop } into g
select new TReport
{
BodyshopId = g.Key.Bodyshop.Id,
Month = g.Key.Month,
MonthAllJobTotal = g.Count()
};
return query2;
}
public static class MyExtensions
{
public static IQueryable<Invoice> Recent(this IQueryable<Invoice> context,long? manufacturerId=null,long? regionId=null,long? vehicleId=null)
{
var now = DateTime.Now;
var today = new DateTime(now.Year, now.Month, 1);
var date1monthago = today.AddMonths(-1);
var date2monthago = today.AddMonths(-2);
var date3monthago = today.AddMonths(-3);
var date4monthago = today.AddMonths(-4);
var date5monthago = today.AddMonths(-5);
var date6monthago = today.AddMonths(-6);
today = TimeManager.EndOfDay(new DateTime(now.AddMonths(-1).Year, today.AddMonths(-1).Month, DateTime.DaysInMonth(now.Year, today.AddMonths(-1).Month)));
var query = from item in context.Invoices.AsNoTracking()
where (item.InvoiceDate >= date12monthago && item.InvoiceDate <= today)
&& (item.InvType == "I" || item.InvType == null)
select item;
if (manufacturerId.HasValue && manufacturerId.Value > 0)
query=query.Where(item=>item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Any(m=>m.ManufacturerId==manufacturerId));
if (regionId.HasValue && regionId.Value > 0)
query=query.Where(item=>item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Any(m=>m.RegionId==regionId.Value));
if (vehicleTypeId.HasValue && vehicleTypeId.Value > 0)
query=query.Where(item=>item.Repair.Job.Vehicle.Model.VehicleTypes.Any(v=>v.Id==vehicleTypeId.Value));
return query;
}
public static IQueryable<Report> ToReport(this IQueryable<Invoice> context)
{
return (from item in query
group item by new { item.InvoiceDate.Month, item.Repair.Job.Bodyshop } into g
select new TReport
{
BodyshopId = g.Key.Bodyshop.Id,
Month = g.Key.Month,
MonthAllJobTotal = g.Count()
});
}
}
var reports=db.Invoices.Recent.ToReport();
var reports=db.Invoices.Recent(ManufacturerEnum.Toyota).ToReport();
var query = context.Invoices.AsNoTracking().Where(item =>
(item.InvType == "I" || item.InvType == null) &&
(item.InvoiceDate >= date12monthago && item.InvoiceDate <= today));
if (regionId.HasValue && regionId.Value > 0)
query = query.Where(item =>
item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Any(source =>
source.ManufacturerId == manufacturerId && source.RegionId == regionId.Value));
else
query = query.Where(item =>
item.Repair.Job.Bodyshop.Manufacturer2Bodyshop.Any(source =>
source.ManufacturerId == manufacturerId));
if (vehicleTypeId.HasValue && vehicleTypeId.Value > 0)
query = query.Where(item =>
item.Repair.Job.Vehicle.Model.VehicleTypes.Any(vehicleType =>
vehicleType.Id == vehicleTypeId.Value);
var query2 = query
.GroupBy(item => new { Month = item.InvoiceDate.Month, BodyshopId = item.Repair.Job.Bodyshop.Id })
.Select(g => new TReport { BodyshopId = g.Key.BodyshopId, Month = g.Key.Month, MonthAllJobTotal = g.Count() });
var result = query2.ToList();