使用 C# linq 实体框架 4。
想从我的波纹管语法中避免 foreach 循环。它能够在 select 语句中计算 foreach 循环计算吗?
var result = (from p in MasterDetail
join q in Master on p.TaxID equals q.TaxID
where p.EffectiveDate >= tempEffectiveDate && p.EffectiveDate <= tempEffectiveDate
select new TransactionItem
{
MasterDetailID = p.MasterDetailID,
CustomCode = p.CustomCode,
TaxID = p.TaxID,
TaxName = q.TaxName,
ItemID = p.ItemID,
EffectiveDate = p.EffectiveDate,
SlavAmount = (decimal)0.0,
SlavTaxAmount = (decimal)0,
Rate = p.Rate,
BarCode = p.BarCode,
IsPercentage = p.IsPercentage,
CurrencyID = p.CurrencyID,
MinAmount = p.MinAmount,
MaxAmount = p.MaxAmount,
}).AsEnumerable();
decimal tempgivenAmount = givenAmount;
decimal temp = 0;
List<TransactionItem> oList = new List<MasterDetailList>();
foreach (MasterDetailList item in result)
{
temp = tempgivenAmount - item.MaxAmount;
if (temp >= 0)
{
temp = item.MaxAmount;
}
else
{
temp = tempgivenAmount;
}
if (item.IsPercentage == true)
{
item.SlavTaxAmount = (decimal)Math.Abs(temp) * (item.Rate / 100);
}
else
{
item.SlavTaxAmount = item.Rate;
}
item.SlavAmount = Math.Abs(temp);
tempgivenAmount = tempgivenAmount - item.MaxAmount;
oList.Add(item);
if (tempgivenAmount <= 0)
{
break;
}
}
如何使用 LINQ 编写上述 foreach?