我正在尝试在多个表上执行联合以产生总加仑数的燃料。有些表只有一列加仑燃料,而其他表有两列或三列。我在Visual Studio 2013 LINQing中使用.NET 4.5和Entity Framework 6进行SQL Server 2012中的数据。
Visual Studio 2013向我提供了'Anonymous Type # 1' does not contain a definition for 'Union' and no extension method 'Union' accepting a first argument of type 'Anonymous Type # 1' could be found (are you missing a using directive or an assembly reference?)
我在其他有关匿名类型的文章中看到的错误,并且仍然不清楚修改代码的最佳方法。 Visual Studio指出的错误位于包含关键字Union的var fueltable
行中,靠近底部。所有“加仑”列的数据类型都是十进制数,因此我不确定要更改的数据类型。我应该将Union放在其他地方,还是改变类型,或完全不同的东西?任何建议都表示赞赏。
public IHttpActionResult FuelTax(FuelTaxBindingModel fuelinfo)
{
...
var trucks = Array.ConvertAll<Trucks ,string>
(fuelinfo.selectedTrucks,
x=>x.FT_Tractor.ToString() ).ToArray();
var temptable1 = (from t1 in db.FuelTaxDetails
join t2 in db.FuelTaxes
on t1.FTD_FT_Key equals t2.FT_Key
join t3 in db.OtherFuelTransactions
on t2.FT_API_Apl_Key equals t3.api_key
where (t2.FT_API_Apl_Key == fuelinfo.apikey)
&& (t2.FT_LoadDate >= fuelinfo.beginningDate
&& t2.FT_LoadDate <= fuelinfo.endDate)
&& (trucks.Contains(t2.FT_Tractor))
select new { t1.FTD_State, t1.FTD_Miles, t1.FTD_NonToll,
t1.FTD_Toll, t3.Gallons };
var temptable2 = (from a in db.FuelTaxDetails
join b in db.FuelTaxes
on a.FTD_FT_Key equals b.FT_Key
join t4 in db.ComdataTranslations
on b.FT_API_Apl_Key equals t4.API_Key
join t5 in db.ComdataTransactions
on t4.Comdata_Key equals t5.CDNCompanyCode
let Gallons = t5.NumberofTractorGallons
+ t5.NumberofReeferGallons
+ t5.OtherFuelGallons
where (b.FT_API_Apl_Key == fuelinfo.apikey)
&& (b.FT_LoadDate >= fuelinfo.beginningDate
&& b.FT_LoadDate <= fuelinfo.endDate)
&& (trucks.Contains(b.FT_Tractor))
select new { a.FTD_State, a.FTD_Miles,
a.FTD_NonToll, a.FTD_Toll,
Gallons });
...
var fueltable = temptable1.Union(temptable2)
.OrderBy(s => s.FTD_State)
.GroupBy(s => new { s.FTD_State })
.Select(g => new
{
FTD_State = g.Key.FTD_State,
FTD_Miles = g.Sum(x => x.FTD_Miles),
FTD_NonToll = g.Sum(x => x.FTD_NonToll),
FTD_Toll = g.Sum(x => x.FTD_Toll),
Gallons = g.Sum(x => x.Gallons)
});