2

所以我有这个相当复杂的 LINQ 查询:

        // 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);

它在 y.SalesValue 上崩溃,说它不能设置为十进制值。我该如何解决?我尝试了以下方法:

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

以下是相关的实体框架代码优先定义:

[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; }
}
4

2 回答 2

0

Enumerable.Sum的表达式参数不能返回浮点数。由于它可以处理单数、小数、双数等,因此它不知道将其隐式转换为哪个。你必须明确地转换它。

尝试这个

 orderby itemSalesGroup.Sum(y => (single) (y.SalesValue)) descending
于 2013-06-05T20:58:38.270 回答
0

在您的实体中,您有 Decimal 类型,并且您从 SQL 返回单一类型。更新您的 SQL 查询(更好)或您的实体。

于 2017-01-24T06:19:52.700 回答