我有这个代码,它应该填满适当的表,它填充第一个表(Mu_Reports
)一个,但第二个表(MU_By_Machine
)一直空白
using (var db = new ProductionContext())
{
var objct = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)db).ObjectContext;
objct.ExecuteStoreCommand("TRUNCATE TABLE [MU Report]");
objct.ExecuteStoreCommand("TRUNCATE TABLE [MU By Machine]");
db.SaveChanges();
var query = db.Production_Reports
.GroupBy(x => new { x.Date, x.Machine_Number, x.Shift })
.Select(x => new
{
Date = x.Key.Date,
Shift = x.Key.Shift,
MachineNum = x.Key.Machine_Number,
MU = x.Sum(i => i.Partial_MU_ * 100)
}).ToList();
foreach (var item in query)
{
var z = new MU_Report();
z.Date = System.Convert.ToDateTime(item.Date);
z.Shift = item.Shift;
z.Machine_Number = item.MachineNum;
z.MU = item.MU;
db.MU_Reports.Add(z);
db.SaveChanges();
}
var query2 = from b in db.MU_Reports
join downtime in db.Downtime_Reports on b.Shift equals downtime.Shift
where downtime.Downtime_Code.Equals("9185")
group downtime by new { b.Date, b.Shift, b.Machine_Number, b.MU } into g
select new
{
Date = g.Key.Date,
Shift = g.Key.Shift,
Machine = g.Key.Machine_Number,
MU = g.Key.MU,
No_Work_Hours = g.Sum(x => x.Total_DownTime)
};
foreach (var item in query2)
{
var y = new MU_By_Machine();
y.Date = item.Date;
y.Shift = item.Shift;
y.Machine_Number = item.Machine;
y.MU = item.MU;
y.MU_w_o_No_Work = (item.MU * 8) / (8 - item.No_Work_Hours);
db.MU_By_Machines.Add(y);
db.SaveChanges();
}
}
我不知道是不是因为我不能让query
和query2
进入同一个按钮,或者我只是做错了什么。仅MU_By_Machine
出现空白时才会出现错误。请记住我是C#的新手。
答案 0 :(得分:2)
您还需要在.ToList()
的select语句末尾调用query2
。