将单独的求和函数添加到 IQueryable LINQ 语句
我希望使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的东西:
编辑:
抱歉,我刚刚注意到
GetTotalReg
调用使用了不同的实体。我会修改我的答案。更新: TotalReg 现在是从
data.Times
求和的Something like this:
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