I fully understand this error, but is there a way to implement some code so that linq for objects understands that my user type should be considered as a primitive type decimal?
I created my own type Money, which is nullable decimalfor one of my constructor overloads. It has an implicit operator overload and all other types of cool jazz. I want to use this custom type in my linq objects, but alas, there is an error in the header.
Is there an attribute that I can declare in my type Moneythat says: "Hey, for the purposes of linq-predictions, I'm from a primitive type decimal"?
Is there any way to create a tree / visitor for a custom expression to declare that all types Moneyshould be considered primitives decimalfor linq predictions?
[HeyImADecimalForLinqProjections]
public class Money : IComparable<Money>
{
public Money()
{
Value = decimal.Zero;
}
public Money(decimal? value)
{
if (value.HasValue)
Value = value.Value;
else
Value = decimal.Zero;
}
public static implicit operator Money(decimal value)
{
return new Money(value);
}
public static implicit operator Money(decimal? value)
{
return new Money(value.Value);
}
...
}
Projection Example:
var items = orderQuery.Where(a => a.OrderId = 345)
.Select(a => new OrderModel()
{
OrderId = a.OrderId,
SubtotalIncludingTax = a.SubtotalIncludingTax, // no dice, SubtotalIncludingTax is of type Money - Unable to cast the type 'System.Decimal' to type 'Money'. LINQ to Entities only supports casting EDM primitive or enumeration types.
SubtotalIncludingTaxRaw = a.SubtotalIncludingTax, // this is fine because SubtotalIncludingTaxRaw is of type decimal
})
.ToList();
My current solution works, but I hate it. First, I design an anonymous type, then project an anonymous type on my actual model. This example is heavily irrigated, and this dual projection solution leads to a lot of repetitions that I would rather avoid.
var anonymousItems = orderQuery.Where(a => a.OrderId = 345)
.Select(a =>
{
OrderId = a.OrderId,
SubtotalIncludingTax = a.SubtotalIncludingTax
.ToList();
var items = anonymousItems
.Select(a => new OrderModel()
{
OrderId = a.OrderId,
SubtotalIncludingTax = a.SubtotalIncludingTax
.ToList();