我有 LINQ 查询检查查询结果并结束语句。该数据库有大约 600k 数据。下面是 LINQ 查询和 if 语句。
var feetypelist = from feetype in dbDataContext.tbl_fee_types
orderby feetype.seq
select feetype.id;
foreach (var fty in feetypelist)
{
var checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
where fee.arr == arr && fee.dep == dep && fee.feetypeid == fty && fee.site_id == siteId
orderby fee.seq
select fee;
if (checkFeeCharges.Count() == 0)
{
checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
where fee.arr == arr && fee.dep == dep && fee.feetypeid == fty && fee.site_id == siteId
&& fee.country == country
orderby fee.seq
select fee;
}
if (checkFeeCharges.Count() == 0)
{
checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
where fee.site_id == siteId && fee.country == country &&
((fee.arr == arr && fee.dep == dep) || (fee.arr.Equals("DOM") && fee.dep.Equals("DOM")) ||
(fee.arr.Equals("*") && fee.dep == dep) || (fee.arr == arr && fee.dep.Equals("*")))
&& fee.feetypeid == fty
orderby fee.seq
select fee;
if (checkFeeCharges.Count() == 0)
{
var country_route = from route in dbDataContext.tbl_country_routes
where route.origin_airport_cd == arr && route.destination_airport_cd == dep
select route;
if (country_route.Count() >= 1)
{
foreach (var c in country_route)
{
if (c.air_asiax == 'Y')
{
//con_flight = "Y";
checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
where fee.site_id == siteId && fee.country == country &&
((fee.arr == "AAX" && fee.dep == "AAX") || (fee.arr == "*" && fee.dep == dep) ||
(fee.arr == arr && fee.dep == "*")) && fee.feetypeid == fty
orderby fee.seq
select fee;
if (checkFeeCharges.Count() == 0)
{
checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
where fee.site_id == siteId && fee.country == country &&
((fee.arr == arr && fee.dep == dep) || (fee.arr == "INT" && fee.dep == "INT") ||
(fee.arr == "*" && fee.dep == dep) || (fee.arr == arr && fee.dep == "*") ||
(fee.arr == country && fee.dep == country)) &&
fee.feetypeid == fty
orderby fee.seq
select fee;
}
}
else
{
checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
where fee.site_id == siteId && fee.country == country &&
((fee.arr == arr && fee.dep == dep) || (fee.arr == "INT" && fee.dep == "INT") ||
(fee.arr == "*" && fee.dep == dep) || (fee.arr == arr && fee.dep == "*") ||
(fee.arr == country && fee.dep == country)) &&
fee.feetypeid == fty
orderby fee.seq
select fee;
}
}
}
else
{
checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
where fee.site_id == siteId && fee.country == country &&
((fee.arr == arr && fee.dep == dep) || (fee.arr == "INT" && fee.dep == "INT") ||
(fee.arr == "*" && fee.dep == dep) || (fee.arr == arr && fee.dep == "*")) &&
fee.feetypeid == fty
orderby fee.seq
select fee;
}
}
}
if (checkFeeCharges.Count() == 0)
{
checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
where fee.site_id == siteId && fee.country == country &&
((fee.arr == arr && fee.dep == dep) || (fee.arr == "INT" && fee.dep == "INT") ||
(fee.arr == "*" && fee.dep == dep) || (fee.arr == arr && fee.dep == "*")) &&
fee.feetypeid == fty
orderby fee.seq
select fee;
}
查询执行需要一段时间才能完成,知道如何优化此查询。
返回 json 请求
if (checkFeeCharges.Any())
{
List<FeeAndChargesModel_Fee_Schedule> feeTable = new List<FeeAndChargesModel_Fee_Schedule>();
foreach (var p in checkFeeCharges)
{
feeTable.Add(
new FeeAndChargesModel_Fee_Schedule()
{
arr = FlightScheduleAPIController.GetCountryName(arr, siteId),
dep = FlightScheduleAPIController.GetCountryName(dep, siteId),
country = p.country,
feedesc = p.feedesc,
feetype = p.feetype,
currency = p.currency,
value = p.value,
remark = p.remark
}
);
label = p.feetype;
}
mainJson.Add(
new FeeAndChargesModel_Main
{
label = label,
con_flight = FlightScheduleAPIController.GetCountryName(GetConnectingFlight(dep, arr), siteId),
details = feeTable
}
);
}
}
return mainJson;