0

I am doing a query for 2 different integers and at least one them needs to be over 0. That works fine by when I'm doing the select statement I compute the stock based on those with the .Sum() method which gives me a null exception if either int is null which that needs to return 0.

I know about DefaultIfEmpty(0) but all the other questions did it differently then how I'm doing it and I'm computing 2 different variables in the select statement and I'm not experienced enough to be able to rework this query.

public IEnumerable<Item> GetInStockTooling()
{
    try
    {
        using (var context = new SpectrumContext())
        {
            var inStock = context.Item
                .Include(i => i.ItemType)
                .Include(i => i.ItemLots)
                .Include(i => i.LocationStocks)
                .Where(w => w.ItemLots.Any(a => a.Quantity > 0 && a.JobItemID == null) || w.LocationStocks.Any(a => a.AmountOfStock > 0) && w.ItemTypeID == (int)ItemTypes.Tooling && w.IsActive)
                .Select(b => new
                {
                    b,
                    ItemLots = b.ItemLots.Where(w => w.JobItemID == null && w.Quantity > 0),
                    ItemType = b.ItemType,
                    LocationStocks = b.LocationStocks.Where(w => w.AmountOfStock > 0)
                })
                .AsEnumerable()
                .Select(x => x.b)
                .ToList()
                .Select(x => { x.Stock = x.ItemLots.Sum(s => s.Quantity) + x.LocationStocks.Sum(s => s.AmountOfStock); return x; });

            return inStock;
        }
    }
    catch (Exception)
    {
        throw;
    }
}

I've tried to do ?? 0 and ?? 0m on on quantity/AmountOfStock but I get an Operand cannot be applied to type 'decimal' and 'decimal' error.

I've also tried using DefaultIfEmpty but I have no idea where to use it considering they are doing all the computing inside the Select and I don't know how I would pull it out of the Select for use of DefaultIfEmpty.

geothachankary
  • 1,040
  • 1
  • 17
  • 30
  • Since we can't tell which "other questions" you looked at and didn't understand, this is going to be quite the shotgun experience. Did you try any of the answers in [Linq query with nullable sum](http://stackoverflow.com/q/696431/215552)? – Heretic Monkey Oct 27 '16 at 17:53
  • Why do you have a `Select` that creates an anonymous class that you just through away with the `Select(x => x.b)`? – juharr Oct 27 '16 at 17:54
  • @MikeMcCaughan Sorry, I'll write what I've tried. Juharr I don't know. This was here before my time. I'm still a beginner so I understand this sort of but the syntax is confusing for me. – user4648142 Oct 27 '16 at 17:58

2 Answers2

0

How about something like this?

x.ItemLots.Sum(s => s.Quantity ?? 0) + x.LocationStocks.Sum(s => s.AmountOfStock ?? 0);

Edit...

Sorry, I didn't read all the way to the end of the question. One thing that might help debug this is to comment out the very last line of the big linq statement (ending it after ToList()), and setting a breakpoint there to see what the result is. That may help clarify what is being looked at so that you'll know how to fix it.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Randy Rowell
  • 37
  • 1
  • 1
  • 5
  • Or `x.ItemLots.Sum(s => s.Quantity) ?? 0 + x.LocationStocks.Sum(s => s.AmountOfStock) ?? 0` – juharr Oct 27 '16 at 17:56
  • I've tried that. I got an error "Operator '??' cannot be applied to type 'decimal' and 'int'. If I change it to 0m I get the same error but instead of int it's decimal – user4648142 Oct 27 '16 at 17:57
  • @user4648142 Maybe only one of `Quantity` or `AmountOfStock` is nullable and you'd only use it on that one. – juharr Oct 27 '16 at 17:59
  • @juharr Either one can be nullable. The way this is supposed to work is that either one can be nullable and as long as either one is over 0 it'll work. But since that means one can be null it screws up. – user4648142 Oct 27 '16 at 18:02
  • @user4648142 Try `?? 0m` instead of `?? 0`. – juharr Oct 27 '16 at 18:22
  • Operand cannot be applied to type 'decimal' and 'decimal'" when I do 0m – user4648142 Oct 27 '16 at 18:25
0

Try to check for null then return 0 else return the number. Using ternary operator here you go:

 x.ItemLots.Sum(s => { return s.Quantity == null ? 0 : s.Quantity; }) + x.LocationStocks.Sum(s => { s.AmountOfStock == null ? 0 : s.AmountOfStock; });
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76