0

I have the following code..

 var GetStock = db.tabStocks.Where(x => x.SourceDocRef == InvoiceNumber.Text);
                
                List<tabStock> tbs = new List<tabStock>();



                foreach (var Stk in GetStock)
                {
                    switch (GetStock.Any(y => y.ProductSKU == Stk.ProductSKU && y.Id != Stk.Id))
                    {

                        case true:

                            if (!(tbs.AsEnumerable().Any(x => x.Id == Stk.Id))) {

                                tbs.Add(Stk);
                            }//tbs.AddRange(GetStock2.Where(g => g.ProductSKU == Stk.ProductSKU && g.Id != Stk.Id));
                            var GetOthers = GetStock.Where(x => x.Id != Stk.Id && x.ProductSKU == Stk.ProductSKU );
                            foreach (var Gt in GetOthers) {

                                

                                    if (!(tbs.AsEnumerable().Any(x => x.Id == Gt.Id))) {

                                        tbs.Add(Gt);

                                    }
                            }   break;




                    }


                }

                //Group and Remove tbs
                Literal1.Text += tbs.AsEnumerable().Count();

                try
                {
                    var GroupIt = tbs.GroupBy(x => x.ProductSKU ).AsEnumerable()
                                       .Select(g => new tabStock
                                       {
                                           ProductName = g.FirstOrDefault().ProductName,
                                           ProductSKU = g.FirstOrDefault().ProductSKU,
                                           Qty = g.Sum(n => n.Qty).Value,
                                           OutQty = g.Sum(n => n.Qty).Value,
                                           Bal = (db.tabStocks.Any(x => x.ProductSKU == g.FirstOrDefault().ProductSKU && x.SourceDocRef != g.FirstOrDefault().SourceDocRef && x.Id < g.OrderBy(t => t.Id).FirstOrDefault().Id) ? db.tabStocks.Where(x => x.ProductSKU == g.FirstOrDefault().ProductSKU && x.SourceDocRef != g.FirstOrDefault().SourceDocRef && x.Id < g.OrderBy(t => t.Id).FirstOrDefault().Id).OrderByDescending(x => x.Id).FirstOrDefault().Bal - g.Sum(f => f.Qty).Value : (0 - g.Sum(f => f.Qty)).Value).Value,
                                           TransactionRef = g.FirstOrDefault().TransactionRef,
                                           CreatedBy = g.FirstOrDefault().CreatedBy,
                                           CreationDate = g.FirstOrDefault().CreationDate,
                                           SourceDocRef = g.FirstOrDefault().SourceDocRef,
                                           InQty = 0,
                                           transactionType = "Sale",
                                           UnitCost = g.FirstOrDefault().UnitCost.Value,
                                           Received = 0,
                                           TotalValuation = 0,
                                           








                                       });

                    foreach (var NewStk in GroupIt.AsEnumerable())
                    {
                        tabStock stk = new tabStock();

                        stk.ProductName = NewStk.ProductName;
                        stk.ProductSKU = NewStk.ProductSKU;
                        stk.Qty = NewStk.Qty;
                        stk.OutQty = NewStk.OutQty;
                        stk.Bal = NewStk.Bal;
                        stk.transactionType = NewStk.transactionType;
                        stk.TransactionRef = NewStk.TransactionRef;
                        stk.CreatedBy = NewStk.CreatedBy;
                        stk.CreationDate = NewStk.CreationDate;
                        stk.SourceDocRef = NewStk.SourceDocRef;
                        stk.InQty = NewStk.InQty;
                        stk.UnitCost = NewStk.UnitCost;
                        stk.Received = 0;
                        stk.TotalValuation = 0;

                        


                        db.tabStocks.Add(stk);

                    }

                    foreach (var OldStk in tbs.AsEnumerable())
                    {

                        db.tabStocks.Remove(OldStk);
                    }
                    db.SaveChanges();

For some reason i have this error: Unable to create a constant value of type 'Type'. Only primitive types or enumeration types are supported in this context

Can anyone help me point to what this could be. I have looked at a lot of others related, nothing seems to work.. toList(), AsEnumerable(). Where is my code wrong please.

noetico
  • 25
  • 6
  • Does this answer your question? [LINQ, Unable to create a constant value of type XXX. Only primitive types or enumeration types are supported in this context](https://stackoverflow.com/questions/13405568/linq-unable-to-create-a-constant-value-of-type-xxx-only-primitive-types-or-enu) – Charlieface Jan 20 '21 at 16:15
  • A `switch(boolValue)` instead of `if`, really?? – Charlieface Jan 20 '21 at 16:16
  • ok will move to `if` sorry, just became obsessed with the switch. Thanks. – noetico Jan 20 '21 at 16:38
  • @Charlieface I have looked at that code and cannot find where a non-premitive type occurs in my code. Any hints? – noetico Jan 20 '21 at 16:45
  • On which line is the error? – Charlieface Jan 20 '21 at 16:49
  • it occurs here: ` foreach (var NewStk in GroupIt.AsEnumerable())` and also a reference to: `var GroupIt = tbs.GroupBy(x => x.ProductSKU )` – noetico Jan 20 '21 at 16:52

0 Answers0