繁体   English   中英

Y的X属性不能设置为“十进制”值。 您必须将此属性设置为“ Single”类型的非空值

[英]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'

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

Enumerable.Sum的表达式参数不能返回浮点数。 由于它可以使用单打,小数,双打等格式,因此它不知道将其隐式转换为哪个。 您必须显式转换它。

尝试这个

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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