IQueryable Casting错误在“选择新”处

本文关键字:选择新 选择 Casting 错误 IQueryable | 更新日期: 2023-09-27 18:30:50

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

IQueryable Casting错误在“选择新”处

您缺少您的类型: 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; }

例如

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