所以我有这个相当复杂的 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; }
}