首页 > 解决方案 > C# EntityFramework 6.0 查询 Decimal > 0 没有结果

问题描述

所以我在这里设置了模型:

public class Stock
{
    [Key]
    //[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public Guid StockId { get; set; } = Guid.NewGuid();
    .....
    public decimal Remaining { get; set; }
}

还有流畅的映射:

    protected override void OnModelCreating(DbModelBuilder mb)
    {
        base.OnModelCreating(mb);
        mb.HasDefaultSchema("dbo");

        mb.Entity<Stock>().Property(m => m.Remaining).HasPrecision(16, 3);
        ....
    }

所以精度是小数点后3位。

当我用 linq 查询时,这很奇怪:

IQueryable<StockDetail> stocks = (from s in db.Stocks .... where s.Remaining > 0);
stocks.Any() --> is false;

问题是,where s.Remaining > 0如何解决?


编辑

实际查询:

IQueryable<StockDetail> stocks = (from s in db.Stocks
    join items in db.Items on s.Item equals items.ItemId
    join types in db.ItemTypes on s.ItemType equals types.ItemTypeId
    join colors in db.ItemColors on s.ItemColor equals colors.ItemColorId
    join units in db.ItemUnits on s.Unit equals units.ItemUnitId
    join buyers in db.Buyers on new { Buyer = s.Buyer } equals new { Buyer = (Guid?)buyers.BuyerId } into temp2
    from buyers in temp2.DefaultIfEmpty(null)
    join suppliers in db.Suppliers.DefaultIfEmpty() on new { Supplier = s.Supplier } equals new { Supplier = (Guid?)suppliers.SupplierId } into temp3
    from suppliers in temp3.DefaultIfEmpty(null)
    join op in db.UserProfiles on s.Operator equals op.UserId
    join curr in db.Currencies on s.Currency equals curr.CurrencyId
    let sales = db.Sales.Where(m => m.SId == s.Sales).FirstOrDefault()
    join parent in db.Stocks on s.Parent equals parent.StockId
    where s.Remaining > 0 
    select new StockDetail()
    {
        Buyer = buyers != null ? (Guid?)buyers.BuyerId : null,
        BuyerName = buyers != null ? buyers.Name : null,
        Supplier = suppliers.SupplierId,
        SupplierName = suppliers.Name,
        Code = s.Code,
        Color = colors.ItemColorId,
        ColorCode = colors.Code,
        ColorName = colors.Color,
        DateCreated = s.DateCreated,
        Gramation = s.Gramation,
        Item = items.ItemId,
        ItemName = items.Name,
        LastEdited = s.LastEdited,
        Operator = op.UserId,
        OperatorName = op.UserName,
        PO = s.PO,
        Remaining = s.Remaining,
        SC = s.SC,
        Qty = s.Qty,
        Setting = s.ItemSetting,
        StockId = s.StockId,
        Type = types.ItemTypeId,
        TypeName = types.Type,
        Unit = units.ItemUnitId,
        UnitName = units.Unit,
        Lot = s.Lot,
        AvgPrice = s.AvgPrice,
        Currency = curr.CurrencyName,
        CurrencyId = s.Currency,
        Note = s.Note,
        Purchase = s.Purchase,
        Sales = s.Sales,
        POIn = s.POIn,
        FromFactory = s.FromFactory,
        OutDeliveryNo = sales != null ? sales.InvoiceNo : "",
        Spec = s.Spec,
        DesignCode = s.DesignCode
    });

标签: c#entity-frameworklinq

解决方案


推荐阅读