如何将计算字段添加到SQL列表

时间:2016-04-08 20:09:12

标签: c# sql linq

我正在跟踪卡车的燃料。

        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循环的结果并将它们添加到我之前的查询/列表中,以便它们显示在报告中?

2 个答案:

答案 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);