2013-06-05 35 views
2

所以我有这个相当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; } 
} 
+1

'y.SalesValue'的数据类型是什么?另外,当你说,“这个解决方案这次不工作”,你是什么意思?有错误吗? –

+0

y.SalesValue当前设置为类型“float”,我得到与“解决方案”相同的错误。 – Bill

+0

在*之前将可空值合并为非空值*,而不是在之后。 – Servy

回答

0

Enumerable.Sum表达参数不能返回浮动。既然它可以与单打,小数,双打等工作,它不知道哪个隐式地将其投入。你必须明确地施放它。

试试这个

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

试过了,不幸的是无法正常工作。 – Bill

0

在你的实体您有小数类型,并正在从SQL返回单一类型。 更新您的SQL查询(更好)或您的实体。

相关问题