简体   繁体   English

使用 group by 从一个包含批量记录的表中获取一些统计数据并将结果放入第二个表中

[英]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.

相关问题 LINQ - 从一个表中获取记录,与第二个表多记录匹配 - LINQ - Get records from one table, with second table multi record matching EF。 根据第二个表中记录的属性从一个表中选择记录,而第二个表中记录的属性为FK - EF. Select records from one table based on the properties of the records from second table that have an FK from the first 如何从第二个表中获取多个记录基于第一个表中的记录列表使用EF - How to get multiple Records from Second table base on list of record from First table Using EF 从二级表(实体框架)获取数据 - Getting data from a second level table (entity framework) MySql C#将数据从一张表复制到另一张表 - MySql C# copy data from one table to a second 从数据表的SQL批量复制将不会上传 - sql bulk copy from data table will not upload 结合两个数据表并使用linq从第二个表中获取金额 - Combining two datatable and getting the amount from second table by using linq 使用Selenium从表中获取数据 - Getting data from table using Selenium 将SQL数据从磁盘上的表批量复制到内存中的表 - Bulk copy of SQL data from table on disk to an in-memory table 如何将一些数据从一个表复制到另一个表? - How to copy some data from one table to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM