gpt4 book ai didi

c# - Y 上的 X 属性无法设置为 'Decimal' 值。您必须将此属性设置为类型为 'Single' 的非空值

转载 作者:太空宇宙 更新时间:2023-11-03 16:12:32 26 4
gpt4 key购买 nike

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

以下是相关的 Entity Framework 代码优先定义:

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

最佳答案

Enumerable.Sum 的表达式参数无法返回 float 。因为它可以处理单数、小数、双数等,所以它不知道将其隐式转换为哪个。你必须显式地转换它。

试试这个

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

关于c# - Y 上的 X 属性无法设置为 'Decimal' 值。您必须将此属性设置为类型为 'Single' 的非空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16949271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com