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

  • Home
  • SEARCH
  • 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 5933219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:56:36+00:00 2026-05-22T14:56:36+00:00

Can this code be written so that items in the list with a parent

  • 0

Can this code be written so that items in the list with a parent property of null will be returned when comparing to an object (x) that also has a null Parent?

MyObject obj = objList.FirstOrDefault(o => n.Parent.Equals(x.Parent));

Assuming the “Equals” method is correctly overridden, this fails where there is an item in the “objList” with a null Parent – with an “Object reference not set to an instance of an object.” exception.

I would assume that occurs because if n.Parent is null, you can’t call its Equal method.

Anyway, I currently resorted this this approach:

MyObject obj = null;
foreach (MyObject existingObj in objList)
{
    bool match = false;

    if (x.Parent == null)
    {
        if (existingObj.Parent == null)
        {
            match = true;
        }
    }
    else
    {
        if (existingObj.Parent != null)
        {
            if (x.Parent.Equals(existingObj.Parent))
            {
                match = true;
            }
        }
    }

    if (match)
    {
        obj= existingObj;
        break;
    }

So while it does work, it’s not very elegant.

  • 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-22T14:56:37+00:00Added an answer on May 22, 2026 at 2:56 pm

    This has nothing to do with FirstOrDefault, but it is a common problem that is solved by the static Object.Equals method. You want:

    MyObject obj = objList.FirstOrDefault(o => Object.Equals(o.Parent, x.Parent));
    

    Incidentally, that method looks something like this:

    public static bool Equals(Object objA, Object objB) 
    {
        // handle situation where both objects are null or both the same reference
        if (objA == objB)
            return true;
        // both are not null, so if any is null they can't be equal
        if (objA == null || objB == null)
            return false; 
        // nulls are already handled, so it's now safe to call objA.Equals
        return objA.Equals(objB);
    } 
    

    Even if that method didn’t exist, you could still write your assignment this way:

    MyObject obj = objList.FirstOrDefault(x.Parent == null ?
        o => o.Parent == null :
        o => x.Parent.Equals(o.Parent));
    

    That uses a different lambda depending on whether x.Parent is null. If it’s null, it just has to look for objects whose Parent is null. If not, it’s always safe to call x.Parent.Equals and uses a lambda that does so.

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

Sidebar

Related Questions

I have written this code to join ArrayList elements: Can it be optimized more?
I've written this set of code and feel that it's pretty poor in quality.
This code is written on fly, plz ignore syntax mistakes if any. std::list<MY_STRUCT> myList;
How can this code compile? The code below in the operator int CAN access
Is there anyway I can modify this code example #include <stdlib.h> #include <iostream> class
This is a silly question, but you can use this code to check if
How can I make this code work? class Meta @array = [:a,:b] def self.method_missing(name,
How can I rewrite this code to check for all characters including the swedish
How can I write this code more cleanly/concisely? /// <summary> /// Creates a set
How can I improve this code? What has made this long winded is the

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.