所以我有這個相當compelx LINQ查詢:Y上的X屬性不能設置爲「十進制」值。您必須將此屬性設置爲類型的非空值「單」
// 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; }
}
'y.SalesValue'的數據類型是什麼?另外,當你說,「這個解決方案這次不工作」,你是什麼意思?有錯誤嗎? –
y.SalesValue當前設置爲類型「float」,我得到與「解決方案」相同的錯誤。 – Bill
在*之前將可空值合併爲非空值*,而不是在之後。 – Servy