I have the following table structure:
Table Item
Id, ExpectedCost, ExpectedQuantity, ...
Table Scan
Id, ItemId, ActualCost, ActualQuantity, ...
Where ItemId is a foreign key into the Item table. This gives a 1 to many relationship between Item and Scan.
This is translated to C# to give the following classes which are autogenerated:
class Item
{
public int Id { get; set; }
public decimal ExpectedCost { get; set; }
public decimal ExpectedQuantity { get; set; }
public EntitySet<Scan> Scans { get; set; }
}
class Scan
{
public int Id { get; set; }
public int ItemId { get; set; }
public EntityRef<Item> Item { get; set; }
public decimal ActualCost { get; set; }
public decimal ActualQuantity { get; set; }
}
I am trying to extend the Item class to get the discrepancy between the expected and actual values:
public partial class Item
{
public decimal ActualCost
{
get { return Scans.Sum(s => s.ActualCost); }
}
public decimal CostDiscrepancy
{
get { return ActualCost - ExpectedCost; }
}
}
The data is read from the database using Linq To SQL and passed back to a Silverlight client via WCF RIA Services. It's displayed on a data grid. The calculation works as the actual cost and the discrepancy values are displayed on the grid.
The problem I have is that when I try to sort or filter on the calculated columns I get the following error:
Message : Load operation failed for query 'GetItems'. The member 'Item.ActualCost' has no supported translation to SQL.
I've tweaked the ActualCost method, but as long as it calls the Sum Linq extension method I get this error.
What can I do to get the calculated value(s) and allow sorting and filtering on those values?