开发者

Add Seperate Sum Functions to an IQueryable LINQ Statement

开发者 https://www.devze.com 2023-02-05 12:48 出处:网络
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,

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 b开发者_如何学Pythony 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消