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 7073437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:55:46+00:00 2026-05-28T05:55:46+00:00

The following code works good IEnumerable<GroupedSelectListItem> groupList = db.Categories.Select(p => new GroupedSelectListItem() { GroupKey

  • 0

The following code works good

IEnumerable<GroupedSelectListItem> groupList = db.Categories.Select(p => new GroupedSelectListItem()
{
     GroupKey = p.ParentCategory.Name,
     GroupName = p.ParentCategory.Name,
     Text = p.Name,
     Value = p.Name
});

While the below code generates object reference not set to an instance error

List<Category> orderedList = new List<Category>();
var rootList = db.Categories.Where(c => c.ParentCategoryId == null).ToList();
    foreach (var item in rootList)
    {
        orderedList.Add(item);
        if (item.SubCategories.Count != 0)
        {
            foreach (var subcat in item.SubCategories)
            {
                orderedList.Add(subcat);
                if (subcat.SubCategories.Count != 0)
                {
                    foreach (var subsubcat in subcat.SubCategories)
                    {
                        orderedList.Add(subsubcat);
                    }
                }
            }
        }
    }
IEnumerable<GroupedSelectListItem> groupList = orderedList.Select(p => new GroupedSelectListItem()
{
     GroupKey = p.ParentCategory.Name,
     GroupName = p.ParentCategory.Name,
     Text = p.Name,
     Value = p.Name
});

Error Detail

Line 54: IEnumerable groupList = orderedList.Select(p => new GroupedSelectListItem()

NOTE: The table for Categories in database contains element with ParentCategoryId for some records equal to null

  • 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-28T05:55:47+00:00Added an answer on May 28, 2026 at 5:55 am

    The first code snippet is using Linq-to-entities with automatic coalescence. It menas that if p doesn’t have ParentCategory it will work without problem on SQL level. The second example use Linq-to-objects where no such feature exists. You must manually check if ParentCategory property is filled prior to accessing its properites:

    GroupKey = p.ParentCategory != null ? p.ParentCategory.Name : null 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On occasion, the following code works, which probably means good concept, but poor execution.
I have the following validation code processed for my User Model. It works good
I have the following code and it works good: var _data = (from qu
The following code works on my machine, but is it good practice / guaranteed
I have written the following C# code: _locationsByRegion = new Dictionary<string, IEnumerable<string>>(); foreach (string
I have the following code and it works pretty good (other than the fact
I used the following htaccess code to rewrite url to index.php It works good
Good Morning, I have the following line of code: var viewModel = new ClassifiedsBrowseViewModel
The following code works great in IE, but not in FF or Safari. I
The following code works for 90+ % of global security groups, but for one

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.