[英]using group by for getting some statistic data from one table with bulk records and puting the results into the second table
As I explained in title I have 2 tables: 1 - "Leaves" with 21000 rec of leaves for about 50 peoples during 20 years 2- "StatisticLeave" which is empty now i want to use group by for getting some statistic data from table 1 and after doing some calculation (like sum &...) putting the results into the second table.正如我在标题中解释的那样,我有 2 个表格: 1 - “叶子”,在 20 年内为大约 50 人提供 21000 个叶子 2 - “StatisticLeave”现在是空的,我想使用 group by 从表 1 中获取一些统计数据并在进行一些计算(如 sum &...)后将结果放入第二个表中。 I wrote below code:我写了下面的代码:
public ActionResult UserStatistic()
{
var ST = new List<StatisticLeave>();
var resuls = db.Leaves.GroupBy(p => p.Pcode);
foreach (var Pcode in resuls)
{
var statistic = new StatisticLeave();
foreach (var item in Pcode)
{
var used = UsedLeaves(item.Pcode);
var estelaji = db.Leaves.Where(p => p.DLT == Leave.DLType.Estelaji).Sum(p => p.LeaveDays);
var bh = db.Leaves.Where(p => p.DLT == Leave.DLType.Bihoghoogh).Sum(p => p.LeaveDays);
statistic.Yearlyhours = ViewBag.mins/60;
statistic.YearlyDays = ViewBag.days;
statistic.YearEstelaji = estelaji;
statistic.YearBihoghoogh = bh;
statistic.Pcode = item.Pcode;
statistic.Year = item.HijriYear;
statistic.UsedYearLaeve = (used / 60) / 8;
ST.Add(statistic);
}
db.StatisticLeave.AddRange(ST);
}
db.SaveChanges();
return View();
}
when I trace the code i get the following caution:当我跟踪代码时,我得到以下警告:
"System.InvalidOperationException HResult=0x80131509 Message=Unable to translate the given 'GroupBy' pattern. Call 'AsEnumerable' before 'GroupBy' to evaluate it client-side." “System.InvalidOperationException HResult=0x80131509 消息=无法翻译给定的 'GroupBy' 模式。在 'GroupBy' 之前调用 'AsEnumerable' 以在客户端对其进行评估。”
would you please tell me where is the problem or how can i fix it.你能告诉我问题出在哪里或者我该如何解决它。
I changed my code as below and the problem Solved我改变了我的代码如下,问题解决了
public ActionResult UserStatistic(Int32? PCode)
{
var ST = new List<StatisticLeave>();
var results = db.Leaves.Where(p => p.Pcode == PCode).ToLookup(p => p.HijriYear);
foreach (var HijriYear in results)
{
var statistic = new StatisticLeave();
foreach (var item in HijriYear)
{
var used = UsedLeaves(item.Pcode, item.HijriYear);
var estelaji = db.Leaves.Where(x => x.Pcode == item.Pcode).Where(p => p.DLT == Leave.DLType.Estelaji).Where(p => p.HijriYear == item.HijriYear).Sum(p => p.LeaveDays);
var bh = db.Leaves.Where(x => x.Pcode == item.Pcode).Where(p => p.DLT == Leave.DLType.Bihoghoogh).Where(p => p.HijriYear == item.HijriYear).Sum(p => p.LeaveDays);
statistic.Yearlyhours = ViewBag.mins / 60;
statistic.YearlyDays = ViewBag.days;
statistic.YearEstelaji = estelaji;
statistic.YearBihoghoogh = bh;
statistic.Pcode = item.Pcode;
statistic.Year = item.HijriYear;
statistic.UsedYearLaeve = (used / 60) / 8;
ST.Add(statistic);
}
db.StatisticLeave.AddRange(ST);
}
db.SaveChanges();
return View();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.