简体   繁体   中英

The X property on Y could not be set to a 'Decimal' value. You must set this property to a non-null value of type 'Single'

So I have this fairly compelx LINQ query:

        // This query will get the top 10 items sold. 
        IEnumerable<IGrouping<Item, ItemSale>> results = (
            from x in database.ItemSales
            where shiftIDs.Contains(x.shiftID)
            group x by x.item into itemSalesGroup
            orderby itemSalesGroup.Sum(y => y.SalesValue) descending
            select itemSalesGroup
        ).Take(10);

And it's crashing on y.SalesValue saying it could not be set to a decimal value. How do I fix this? I have tried the following:

        orderby itemGroup.Sum(y => (float?)y.SalesValue) ?? 0f descending
        orderby itemGroup.Sum(y => (float?)y.SalesValue ?? 0f) descending

Here are the relevent Entity-Framework Code First definition:

[Table("ItemSales")]
public class ItemSale {
    public int ID { get; set; }
    [Column("ItemID")]
    public int itemID { get; set; }
    [ForeignKey("itemID")]
    public Item item { get; set; }
    [Column("Shifts_ID")]
    public int shiftID { get; set; }
    [ForeignKey("shiftID")]
    public Shift shift { get; set; }
    ...
    public float SalesValue { get; set; }
}

The expression parameter for Enumerable.Sum cannot return a float. Since it can work with singles, decimals, doubles, etc., it doesn't know which to implicitly cast it to. You have to explicitly cast it.

Try this

 orderby itemSalesGroup.Sum(y => (single) (y.SalesValue)) descending

In your entity you have Decimal type and you are returning single type from SQL. Update your SQL query (better) or your entity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM