esnezz
esnezz

Reputation: 669

C# Linq To Entities (SkipWhile not working)

I am calculating inventory based on stock transactions but I want to exclude those stock transactions whose invoice is not marked as IsStocked which is Nullable Boolean, I am trying to achieve this by using SkipWhile which is not working and it simply count all of transactions no matter it's invoice IsStocked property is true or false:

Here is what I tried:

public double GetInStockQuantity(Warehouse warehouse, Entities db)
{
    double res;
    try
    {
        res = db.Stocks.AsNoTracking().Where(c => c.Product.ID == ID && c.WarehouseID == warehouse.ID).
            AsNoTracking().AsEnumerable().
            SkipWhile(c => c.InvoiceItems.Any(q => q.Invoice.IsStocked == false)).Sum(q => q.Quantity);
    }
    catch (Exception)
    {
        res = 0;
    }
    return res;
}

Upvotes: 0

Views: 148

Answers (1)

jayanta
jayanta

Reputation: 163

You can try Where clause.

    public double GetInStockQuantity(Warehouse warehouse, Entities db)
    {
        double res;
        try
        {
            res = db.Stocks.AsNoTracking().Where(c => c.Product.ID == ID &&       c.WarehouseID == warehouse.ID).
            AsNoTracking().AsEnumerable().
            Where(c => c.InvoiceItems.Any(q => q.Invoice.IsStocked == true)).Sum(q => q.Quantity);
        }
        catch (Exception)
        {
           res = 0;
        }
          return res;
   }

Upvotes: 1

Related Questions