locked
LINQ SUM in Computed Property RRS feed

  • Question

  • I have several Computed Properties where I would like to Sum the field 'Landing' where the record field 'Nnumber' equals the in Number on my Screen and the record date is not newer than the date on my screen.  These computed fields will shown the sum of the field previous to the current record.

    My attached SQL Database is named 'FltSataDataData'
    Table is 'FlightLogs', one of the fields to SUM is 'Landing'

    The following code shows an error at sum, 'Definition of method SUM is not accessible in this context'.

     Private Sub ForwardLandings_Compute(ByRef result As Decimal)
                ' Set result to the desired field value
    
                Dim ForwLandings = Aggregate ldg In DataWorkspace.FltStaDataData.FlightLogs Where Nnumber = Me.Nnumber AndAlso
                                  ldg.FlightDate < Me.FlightDate Into sum(ldg.Landing)
    
                result = ForwLandings
            End Sub
    

    Monday, November 14, 2011 1:45 AM

All replies

  • This works over here. I did notice the calc not take place until saved and refreshed, which I was not expecting. I guess in the past doing running sum, I had a parent record that already had child records. In this case, no parent. This kind running sum is fairly expensive. But you need it when you need it. I used integer instead of decimal for Landing type, but you can change as needed.

    partial void FowardLandings_Compute(ref int result)
    {
        result = this.DataWorkspace.ApplicationData.FlightDatas.Where(fd => fd.Number == this.Number && fd.FlightDate < this.FlightDate).Cast<FlightData>().Sum(fd => fd.Landing);
    }

    VB

    Private Partial Sub FowardLandings_Compute(ByRef result As Integer)
    	result = Me.DataWorkspace.ApplicationData.FlightDatas.Where(Function(fd) fd.Number = Me.Number AndAlso fd.FlightDate < Me.FlightDate).Cast(Of FlightData)().Sum(Function(fd) fd.Landing)
    End Sub
    

     

    Monday, November 14, 2011 4:39 AM
  • William,

    I do not understand your code and could not get it to work here.  My table 'FlightLog' from my datebase 'FltSataDataData' is not reflected in your code.

    Thank you.


    Buzz Carter
    Monday, November 14, 2011 2:41 PM
  • Change your code.  I don't have perfect knowledge of your schema, so that was just example of something close. You should be able wiggle around to fit your schema.
    Monday, November 14, 2011 5:51 PM