I have a linq query that executes successfully, one of the columns returned is a decimal type that is used to represent prices in pounds and pence (there will never be any negative values)
I want to be able to strip out the pounds and pence into separate Properties of my projection, however when using functionality such as
var result= from j in context.Products
select
new{
Price = t.Price,
PricePounds = Math.Truncate(t.Price)
};
I get an error that Math.truncate is not supported as it cannot be translated into a store expression. How can I get the pounds value from this query?
If you don’t need to do anything else in the database after that, the simplest approach is just to perform the truncation client-side:
Note that you might also want to just cast to
int– and that might be supported in EF already.EDIT: As noted in comments, you may want to perform a projection first, e.g.
(Where
SomethingElseis another property you’re interested in, as an example – I doubt that you only want the price.)This will avoid the entire entity being fetched when you only want a few properties.