Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 913677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:35:58+00:00 2026-05-15T17:35:58+00:00

When using LINQ to get data from a list I encounter this error. How

  • 0

When using LINQ to get data from a list I encounter this error. How can this be fixed?

Value cannot be null. Parameter name: source

var nCounts = from sale in sal
              select new
              {
                  SaleID = sale.OrderID,
                  LineItem = from sli in sale.LineItems
                             group sli by sli.Item into ItemGroup
                             select new
                             {
                                 Item = ItemGroup.Key,
                                 Weeks = ItemGroup.Select(s => s.Week)
                             }
              };

foreach (var item in nCounts)
{
    foreach (var itmss in item.LineItem)
    {
        // MessageBox.Show(itmss.Item.Name);
        itemsal.Add(new Roundsman.BAL.WeeklyStockList(itmss.Item.Name.ToString(),
                    itmss.Item.Code.ToString(),
                    itmss.Item.Description.ToString(),
                    Convert.ToInt32(itmss.Item.Quantity), 2, 2, 2, 2, 2, 2, 2, 2, 2));
    }                      
}

Error which i got after execution my LINQ I got that type of result (one line, orginally):

System.Linq.Enumerable.WhereSelectListIterator<Roundsman.BAL.Sale,
<>f__AnonymousType1<int,System.Collections.Generic.IEnumerable
<<>f__AnonymousType0<Roundsman.BAL.Items.Item,System.Collections.Generic.IEnumerable
<Roundsman.BAL.WeeklyRecord>>>>>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:35:59+00:00Added an answer on May 15, 2026 at 5:35 pm

    The error you receive is from another method than the one you show here. It’s a method that takes a parameter with the name “source”. In your Visual Studio Options dialog, disable “Just my code”, disable “Step over properties and operators” and enable “Enable .NET Framework source stepping”. Make sure the .NET symbols can be found. Then the debugger will break inside the .NET method if it isn’t your own. then check the stacktrace to find which value is passed that’s null, but shouldn’t.

    What you should look for is a value that becomes null and prevent that. From looking at your code, it may be the itemsal.Add line that breaks.

    Edit

    Since you seem to have trouble with debugging in general and LINQ especially, let’s try to help you out step by step (also note the expanded first section above if you still want to try it the classic way, I wasn’t complete the first time around):

    • Narrow down the possible error scenarios by splitting your code;
    • Replace locations that can end up null with something deliberately not null;
    • If all fails, rewrite your LINQ statement as loop and go through it step by step.

    Step 1

    First make the code a bit more readable by splitting it in manageable pieces:

    // 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 code above is from the top of my head, of course, because I cannot know what type of classes you have and thus cannot test the code before posting. Nevertheless, if you edit it until it is correct (if it isn’t so out of the box), then you already stand a large chance the actual error becomes a lot clearer. If not, you should at the very least see a different stacktrace this time (which we still eagerly await!).

    Step 2

    The next step is to meticulously replace each part that can result in a null reference exception. By that I mean that you replace this:

    select new
    {
        SaleID = sale.OrderID,
        LineItem = GetLineItem(sale.LineItems)
    };
    

    with something like this:

    select new
    {
        SaleID = 123,
        LineItem = GetLineItem(new LineItem(/*ctor params for empty lineitem here*/))
    };
    

    This will create rubbish output, but will narrow the problem down even further to your potential offending line. Do the same as above for other places in the LINQ statements that can end up null (just about everything).

    Step 3

    This step you’ll have to do yourself. But if LINQ fails and gives you such headaches and such unreadable or hard-to-debug code, consider what would happen with the next problem you encounter? And what if it fails on a live environment and you have to solve it under time pressure=

    The moral: it’s always good to learn new techniques, but sometimes it’s even better to grab back to something that’s clear and understandable. Nothing against LINQ, I love it, but in this particular case, let it rest, fix it with a simple loop and revisit it in half a year or so.

    Conclusion

    Actually, nothing to conclude. I went a bit further then I’d normally go with the long-extended answer. I just hope it helps you tackling the problem better and gives you some tools understand how you can narrow down hard-to-debug situations, even without advanced debugging techniques (which we haven’t discussed).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First of all, it's a really bad idea to use… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer If you are not dead set on using a listbox,… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer killproc will terminate programs in the process list which match… May 16, 2026 at 9:17 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am trying to get serialized results from a WCF service from database using
I am using LINQ to Entity in a project, where I pull a bunch
I have implemented a DAL using Rob Conery's spin on the repository pattern (from
I am working with oracle and nhibernate. I can select list of an object
I've been using LINQ for a while now, but seem to be stuck on
Scenario: I have a generic list of Audits and a generic list of AuditImages.
I have a table it includes 3 foreign key field like that: My Table:
I'm currently in the beginning of learning WCF, as some of the concepts and
Attached is a quick program written under interview conditions that is designed to flatten

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.