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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:26:54+00:00 2026-05-11T22:26:54+00:00

I have an odd linq subquery issue. Given the following data structure: Parents Children

  • 0

I have an odd linq subquery issue.

Given the following data structure:

Parents            Children
-------            --------
Id                 Id
                   ParentId
                   Location
                   HasFoo

(obviously this is not the real structure, but it’s close enough for this example)

I’m able to run this query and get a desired result:

bool b = (from p in Parents
          from c in Children
          where p.Id == 1 && c.ParentId == p.Id && c.Location == "Home"
          select c.HasFoo).SingleOrDefault();

So if there is a child that has the Location “Home” for a Parent of Id 1, I will get that Child’s “HasFoo” value, otherwise, I’ll get false, which is the “default” value for a bool.

However, if I try and write the query so I have a list of Parent objects, like so:

var parentList = from p in Parents
                 select new ParentObject
                 {
                   ParentId = p.ParentId,
                   HasHomeChildren = p.Children.Count(c => c.Location == "Home") > 0,
                   HasHomeChildrenWithFoo = (from c in p.Children where c.Location == "Home" select c.HasFoo).SingleOrDefault()
                 }

I get the following error when iterating over the list:

The null value cannot be assigned to a member with type System.Boolean which is a non-nullable value type.

I don’t see where this “null” value is coming from, however.

  • 1 1 Answer
  • 1 View
  • 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-11T22:26:54+00:00Added an answer on May 11, 2026 at 10:26 pm

    I wonder if the compiler is inferring HasHomeChildrenWithFoo to be bool, but then actually casting to a nullable bool (thus messing up your SingleOrDefault call). At any rate, I’d be willing to bet you could fix it with a cast to a nullable type in that final select which you can then manually default to false when null. It’d probably make the error go away, but it’s kind of a brute-force kludge.

    var parentList = from p in Parents
                     select new ParentObject
                     {
                       ParentId = p.ParentId,
                       HasHomeChildren = p.Children.Any(c => c.Location == "Home"),
                       HasHomeChildrenWithFoo = (from c in p.Children where c.Location == "Home" select (bool?)c.HasFoo) ?? false)
                     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an odd issue while using FlashCS4. I have a textfield that, when
I have an odd alignment issue where an extra space is being added. Here
I have an odd issue. I have one PHP script that is used for
I have defined the following classes and methods: using System; using System.Linq.Expressions; using System.Windows.Forms;
I have an odd issue here :http://alarmefast.com/securitasdirect2011/index.php On the top left corner, I have
I have an odd issue happening when trying to open multiple Input Streams (in
I have an odd problem with the following: function loadTextbox(jsonUrl,divId){ $.getJSON(jsonUrl, function(json) { $('#'
I have noticed something odd with linq and the Contains method. It seems to
I have a bunch of data in pipe-delimited format, where odd entries are index
Have an odd issue while using the active_directory module, which is amazing BTW... awesome

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.