“选择新”时出现IQueryable Casting错误

时间:2013-09-09 13:49:02

标签: c# entity-framework

 IQueryable<GRNDetail> selectedResults = from c in InventoryContext.GRNDetails
                              where c.ProductID == pro.ProductID && (c.Quantity - c.SoldQuantity > 0 || c.SoldQuantity.Value.Equals(null))
                              select new
                              {
                                  c.GRNDetailID,
                                  c.GRNID,
                                  c.Product,
                                  c.ProductID,
                                  c.PurchaseOrderDetail,
                                  c.PurchaseOrderDetailID,

                                  c.Quantity,
                                  c.SoldQuantity,
                                  c.Tax,
                                  c.TaxRate,
                                  c.Total,
                                  c.UnitPrice, 
                                  SaleQty = (c.Quantity - c.SoldQuantity)
                              };

1 个答案:

答案 0 :(得分:4)

您缺少类型:选择中的GRNDetail:
select new GRNDetail

编辑: 或者使用Select c;带回整个物体。或者,如果您只想要某些项目,也可以创建自定义类型。

您可以创建一个这样的自定义类型:使用您的SaleQty(设置为货币或双倍或任何适合您需要的数据类型:并将其用于您的结果:

 public class GRNDetailResults
    {
        public int GRNDetailID { get; set; }

        public int GRNID { get; set; }

        etc ....

        public double SaleQty { get; set; }

E.g

IQueryable<GRNDetailResults> selectedResults = from c in InventoryContext.GRNDetails
                              where c.ProductID == pro.ProductID && (c.Quantity - c.SoldQuantity > 0 || c.SoldQuantity.Value.Equals(null))
                              select new GRNDetailResults
                              {
                                 GRNDetailID = c.GRNDetailID,
                                  GRNID  =c.GRNID, 
                                   etc ...
                                  SaleQty = (c.Quantity - c.SoldQuantity)
                              };