1 回答
TA贡献1934条经验 获得超2个赞
您不需要在数据库查询中执行所有操作。
数据库只是IO设备,只负责读写数据。
加载所有需要的数据并计算。
var contractsId = waitingData.Select(data => data.ContractId).ToList();
var pricingCriterias = table4.Where(criteria => contractsId.Contains(criteria.ContractId)
.ToLookup(criteria => criteria.ContractId);
var maxWaitingTime =
pricingCriterias.SelectMany(group => group)
.Where(criteria => criteria.PricingCriterionName = "WaitingTimeMax")
.Max(criteria => criteria.PricingCriterionValue);
foreach (var waitingItem in waitingData)
{
// Calculate others waiting values
var waitingPerPeriod = (WaitingTime - maxWaitingTime) / WaitingTimePeriod);
var waitingPrice = waitingPerPeriod * WaitingTimeOverdue;
var WaitingTimeCost = Math.Min(waitingPrice, OverDuePriceMax)
}
下面的示例与DataTables.
然而,它DataTable是沉重且非强类型的数据结构,作为开发人员需要更多的时间来处理它。
而是将数据转换DataTable为具有描述性属性名称的普通强类型对象 - 您将免费获得 IntelliSense ;)
var contractsId =
waitingData.AsEnumerable()
.Select(row => row.Field<int>("ContractId"))
.ToList();
var pricingCriterias =
table4.AsEnumerable()
.Where(row => contractsId.Contains(row => row.Field<int>("ContractId"))
.ToLookup(row => row.Field<int>("ContractId"));
var maxWaitingTime =
pricingCriterias.SelectMany(group => group)
.Where(row => row.Field<string>("PricingCriterionName") = "WaitingTimeMax")
.Max(row => row.Field<int>("PricingCriterionValue"));
foreach (var waitingItem in waitingData)
{
// Calculate others waiting values
var waitingPerPeriod = (WaitingTime - maxWaitingTime) / WaitingTimePeriod);
var waitingPrice = waitingPerPeriod * WaitingTimeOverdue;
var WaitingTimeCost = Math.Min(waitingPrice, OverDuePriceMax)
}
- 1 回答
- 0 关注
- 164 浏览
添加回答
举报