我正在跟踪卡车的燃料。
List<FuelLightTruckDataSource> data = new List<FuelLightTruckDataSource>();
using (SystemContext ctx = new SystemContext())
{
List<FuelLightTruckDataSource> dataTransfers
= ctx.FuelTransfer
.Where(tr => DbFunctions.TruncateTime(tr.Date) >= from.Date && DbFunctions.TruncateTime(tr.Date) <= to.Date
//&& tr.ToAsset.AssignedToEmployee.Manager
&& tr.ToAsset.AssignedToEmployee != null
&& tr.ToAsset.AssetType.Code == "L"
&& tr.Volume != null)
//&& (tr.FuelProductType.FuelProductClass.Code == "GAS" || tr.FuelProductType.FuelProductClass.Code == "DSL"))
.GroupBy(tr => new { tr.ToAsset, tr.Date, tr.FuelTankLog.FuelProductType.FuelProductClass, tr.FuelTankLog.FuelCard.FuelVendor,tr.Volume, tr.ToAssetOdometer })
.Select(g => new FuelLightTruckDataSource()
{
Asset = g.FirstOrDefault().ToAsset,
Employee = g.FirstOrDefault().ToAsset.AssignedToEmployee,
ProductClass = g.FirstOrDefault().FuelTankLog.FuelProductType.FuelProductClass,
Vendor = g.FirstOrDefault().FuelTankLog.FuelCard.FuelVendor,
FillSource = FuelFillSource.Transfer,
Source = "Slip Tank",
City = "",
Volume = g.FirstOrDefault().Volume.Value,
Distance = g.FirstOrDefault().ToAssetOdometer,
Date = g.FirstOrDefault().Date,
})
.ToList();
在我查询之后,我需要计算消耗率和行进距离。 &#34;结果&#34;将是一系列条目,包括&#34; consumptionRate&#34;和&#34;距离&#34;它匹配上面的查询。
// Get consumption rate data for each asset
foreach (int assetId in assetIds)
{
FuelConsumptionRateQueryResult result = FuelConsumptionRateQueryService.Get(assetId, from, to, AssetCounterType.Odometer);
result.Entries.ToList();
}
我的问题是如何获取foreach循环的结果并将它们添加到我之前的查询/列表中,以便它们显示在报告中?
答案 0 :(得分:0)
您的问题似乎是由于您在result
循环的每次迭代中将foreach
声明为新变量,导致result
被覆盖。此外,result
循环完成后,foreach
不在范围内。
相反,你可能想要更接近的东西:
var allResults = new List<T>; //where T is whatever object type this is (FuelConsumptionRateQueryResult?)
foreach (int assetId in assetIds)
{
//this is a new variable at each iteration of the loop
FuelConsumptionRateQueryResult result = FuelConsumptionRateQueryService.Get(assetId, from, to, AssetCounterType.Odometer);
allResults.Add(result); //add the current result to your list (outside the scope of this loop)
}
基本上,在循环之外声明您的变量,并将Add
每个结果声明到allResults
列表。
答案 1 :(得分:0)
通过LINQ语句创建附加数据并连接两个列表:
var results = assetIds.Select(id => FuelConsumptionRateQueryService
.Get(id, from, to, AssetCounterType.Odometer);
dataTransfers = dataTransfers.Concat(results);