none
How to embed a delegate into Sum function to enable dynamic LINQ RRS feed

  • Question

  • Hi all,

    I have a LINQ, it looks like:

    From PR As Payroll.PayrollsRow In DSPayroll.Payrolls _
    Group By PR.DepartmentNumber _
    Into SumResult = Sum(PR.AcutualWorkingDaysSalary) _
    Order By DepartmentNumber
    
    Then, I hope to replace the Sum function's parameter as a Lambada expression. My idea looks like:
    Dim F = Function(PR) PR.AcutalWorkingDaysSalary + 0.5
    
    Dim V = From PR As Payroll.PayrollsRow In DSPayroll.Payrolls _
           Group By PR.DepartmentNumber _
           Into SumResult = Sum(F) _
           Order By DepartmentNumber
    

    That mean I could change the Sum fields by a functio arguement. I know there is a Dynamic LINQ library to allow me use string expression, but is there any strong type way to make it?

    BR

    Alex


    Alex Du
    Thursday, January 13, 2011 10:51 AM

Answers

  • Just 5 minutes later of posting this, I realized the solution:

    Dim F = Function(PR) PR.AcutalWorkingDaysSalary + 0.5
    
    Dim V = From PR As Payroll.PayrollsRow In DSPayroll.Payrolls _
        Group By PR.DepartmentNumber _
        Into SumResult = Sum(F(PR)) _
        Order By DepartmentNumber
    
    

    The point is, pass the LINQ enumrate varialble into the lambada's parameter, which make the lambada run.


    Alex Du
    Thursday, January 13, 2011 11:33 AM