The error you get is different from the one you show here. This is a method that accepts a parameter named "source". In the Visual Studio Options dialog box, turn off My Only Code, turn off the Step over Properties and Operators, and turn on the Enable Source .NET Framework Step Step. Make sure .NET characters can be found. Then the debugger will break inside the .NET method if it is not your own. then check stacktrace to see which value is passed with a null value, but shouldn't.
What you need to look for is a value that becomes null and prevents this. If you look at your code, it could be line break itemsal.Add .
Edit
Since you seem to have problems with debugging in general and LINQ, especially, try to help you step by step (also pay attention to the extended first section above, if you still want to try the classic way, I was not completely the first time):
- Reduce possible error scenarios by splitting the code;
- Replace places where something
null may be intentionally not null ; - If all else fails, rewrite the LINQ statement as a loop and go through it step by step.
Step 1
First make the code more readable by dividing it into manageable parts:
// in your using-section, add this: using Roundsman.BAL; // keep this in your normal location var nCounts = from sale in sal select new { SaleID = sale.OrderID, LineItem = GetLineItem(sale.LineItems) }; foreach (var item in nCounts) { foreach (var itmss in item.LineItem) { itemsal.Add(CreateWeeklyStockList(itmss)); } } // add this as method somewhere WeeklyStockList CreateWeeklyStockList(LineItem lineItem) { string name = itmss.Item.Name.ToString(); // isn't Name already a string? string code = itmss.Item.Code.ToString(); // isn't Code already a string? string description = itmss.Item.Description.ToString(); // isn't Description already a string? int quantity = Convert.ToInt32(itmss.Item.Quantity); // wouldn't (int) or "as int" be enough? return new WeeklyStockList( name, code, description, quantity, 2, 2, 2, 2, 2, 2, 2, 2, 2 ); } // also add this as a method LineItem GetLineItem(IEnumerable<LineItem> lineItems) { // add a null-check if(lineItems == null) throw new ArgumentNullException("lineItems", "Argument cannot be null!"); // your original code from sli in lineItems group sli by sli.Item into ItemGroup select new { Item = ItemGroup.Key, Weeks = ItemGroup.Select(s => s.Week) } }
The above code, of course, is because of my head, because I donโt know what types of classes you have, and therefore I canโt check the code before publishing. However, if you edit it until it is correct (if it is not so out of the box), then you already have a big chance that the actual error becomes much clearer. If not, this time you should at least see another glass (which we are still expecting!).
Step 2
The next step is to carefully replace each part, which can lead to the exclusion of a null reference. By this I mean that you are replacing this:
select new { SaleID = sale.OrderID, LineItem = GetLineItem(sale.LineItems) };
with something like this:
select new { SaleID = 123, LineItem = GetLineItem(new LineItem()) };
This will create a garbage outlet, but narrows the problem even further to your potential line of violation. Do the same as above for other places in LINQ statements that can end with null (almost everything).
Step 3
You will need to do this step yourself. But if LINQ fails and gives you such headaches and such unreadable or hard to debug code, think about what will happen to the next problem you are facing? And what if he fails in a living environment, and you have to solve it under the pressure of time =
Moral: itโs always good to learn new techniques, but sometimes itโs even better to return to something clear and understandable. Nothing against LINQ, I like it, but in this particular case, give it a break, fix it with a simple cycle and rename it after six months or so.
Conclusion
Actually, there is nothing to conclude. I went a little further than usual with a long extended answer. I just hope this helps you better solve the problem and gives you some tools to understand how you can narrow down hard-debugging situations, even without advanced debugging methods (which we did not discuss).