将单独的求和函数添加到 IQueryable LINQ 语句

发布于 2024-10-13 02:24:32 字数 1219 浏览 6 评论 0原文

我希望使用 IQueryable 数据源填充 Telerik RadGrid,但在该网格内总共包含 5 列。当我创建单独的报告时,我已经为总计构建了单独的函数,但在将它们迭代到我当前的网格 LINQ 语句中时遇到了一些问题。

我当前的 LINQ 语句如下所示:

public static IQueryable GetUnapprovedTimesheets(string Period)
    {
        DataContext Data = new DataContext();

        var Times = (from c in Data.System_Times
                     where c.Period == Period && c.Status == 1
                     orderby c.Employee.LName
                     select c).GroupBy(s => s.EmployeeID).Select(x => x.FirstOrDefault());

        return Times;
    }

该语句获取输入期间的时间表,并显示状态为 1 的记录,然后按 EmployeeID 分组显示这些记录。该语句将所有内容正确地拉入网格中,但我还想添加常规、加班、旅行和分班的总计。

我为动态报告单独构建的函数看起来像这样:

public static decimal? GetTotalReg(int EmployeeID, string Period)
    {
        DataContext Data = new DataContext();
        var Total = (from c in Data.Times
                     where c.EmployeeID == EmployeeID && c.Period == Period && c.PayCode == "211"
                     select c.Hours);

        return Total.Sum();
    }

有没有办法将第二个语句合并到第一个语句中? (请记住,我必须合并的不仅仅是一个,而是 5 个 Sum 语句)

然后,这一语句将用作我的 RadGrid 的 IQueryable 数据源,由 RadGrid 的 DataField=" " 调用。

I'm looking to populate a Telerik RadGrid with an IQueryable DataSource but include 5 Total columns inside that Grid. I have built separate functions for the Totals when I create individual reports, but am having some trouble iterating them into my current LINQ statement for the Grid.

My current LINQ statement looks like this:

public static IQueryable GetUnapprovedTimesheets(string Period)
    {
        DataContext Data = new DataContext();

        var Times = (from c in Data.System_Times
                     where c.Period == Period && c.Status == 1
                     orderby c.Employee.LName
                     select c).GroupBy(s => s.EmployeeID).Select(x => x.FirstOrDefault());

        return Times;
    }

This statement grabs the Timesheets for the Period entered, and shows the records with a status of 1, these are then displayed grouped by EmployeeID. This statement pulls everything into the Grid properly but I would like to also add Totals for Regular, Overtime, Travel, and Sub.

The functions I have built separately for dynamic reporting look something like this:

public static decimal? GetTotalReg(int EmployeeID, string Period)
    {
        DataContext Data = new DataContext();
        var Total = (from c in Data.Times
                     where c.EmployeeID == EmployeeID && c.Period == Period && c.PayCode == "211"
                     select c.Hours);

        return Total.Sum();
    }

Is there any way to incorporate the Second Statement into the First? (Keeping in mind that I will have to incorporate not just one, but 5 Sum statements)

This one statement will then be used as an IQueryable DataSource for my RadGrid, called by DataField=" " of the RadGrid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扶醉桌前 2024-10-20 02:24:32

像这样的东西:

public static IQueryable GetUnapprovedTimesheets(string period)
{
   DataContext data = new DataContext();

   var times = data
                   .Where(a => a.Period == period && a.Status == 1)
                   .OrderBy(a => a.Employee.LName)
                   .GroupBy(a => a.EmployeeID)
                   .Select(
                      a => new{
                               System_Time = a.FirstOrDefault(),
                               TotalReg = data.Times
                                           .Where(b => b.EmployeeID == a.Key 
                                                    && b.Period == period
                                                    && b.PayCode == "211")
                                           .Sum(b => b.Hours)
                              }
                          );

   return times;
}

编辑:
抱歉,我刚刚注意到 GetTotalReg 调用使用了不同的实体。我会修改我的答案。

更新: TotalReg 现在是从 data.Times 求和的

Something like this:

public static IQueryable GetUnapprovedTimesheets(string period)
{
   DataContext data = new DataContext();

   var times = data
                   .Where(a => a.Period == period && a.Status == 1)
                   .OrderBy(a => a.Employee.LName)
                   .GroupBy(a => a.EmployeeID)
                   .Select(
                      a => new{
                               System_Time = a.FirstOrDefault(),
                               TotalReg = data.Times
                                           .Where(b => b.EmployeeID == a.Key 
                                                    && b.Period == period
                                                    && b.PayCode == "211")
                                           .Sum(b => b.Hours)
                              }
                          );

   return times;
}

Edit:
Sorry, I just noticed the GetTotalReg call uses a different entity. I'll modify my answer.

Update: TotalReg is now summed from data.Times

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文