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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:28:19+00:00 2026-05-28T15:28:19+00:00

I have a LINQ to SQL query which produces an object of type {System.Data.Linq.DataQuery}.

  • 0

I have a LINQ to SQL query which produces an object of type {System.Data.Linq.DataQuery}. My problem is I am getting an exception when trying to determine if the list is empty (or null).

I tried various different tests, and searched through StackOverflow for an answer but nothing I tried will get me past this exception:

  // I try to declare explicitly:
   IQueryable<DataAccess.Entities.CompanyProfile> carrierCodes = null;


  // and implicitly
         var carrierCodes = from cc in context.CompanyProfiles
                          where cc.ProfileTypeID == 9 &&
                                cc.CompanyProfileCode == shipTEntity.CargoControlNum.Substring(0, 3) &&
                                cc.CompanyProfileID == shipTEntity.CompanyProfile.CompanyProfileID
                          select cc;



        // VAROIUS NULL REF TESTS
        var _info = carrierCodes.FirstOrDefault( u => u != null); // fails

        int itemCount = Enumerable.Cast<string>(carrierCodes.DefaultIfEmpty()).Count(); // fails

       var InstanceCount = carrierCodes.Count(i => i != null); // fails

        foreach (var companyProfile in carrierCodes) // fails
        {
            if ( companyProfile != null )
                itemCount++;
        }

        var  exists = carrierCodes.DefaultIfEmpty().ToList(); // fails

        int count = exists.Count;

        return count > 0;

As you can see, I tried FirstOrDefault, testing for null (if carrierCodes != null) but that evaluates to false every time. I tried to get the instance count various ways, but since the list is null it gives me an exception when trying to test the count.

What is confusing is that the carrierCode list is not null, but it gives a Object Null reference exception.

PS This is the exception:

 "Object reference not set to an instance of an object."

Thank you,

Confused in Seattle.

UPDATE

I’ve tried the following suggestions – they all produce the same exception as before. I’ve attached the exception dialog and exception details:

  var carrierCodes = from cc in context.CompanyProfiles
                           where cc.ProfileTypeID == 9 &&
                                 cc.CompanyProfileCode == shipTEntity.CargoControlNum.Substring( 0, 3 ) &&
                                 cc.CompanyProfileID == shipTEntity.CompanyProfile.CompanyProfileID
                           select cc;

         // the below all throw ex:
        var isValid = carrierCodes.Count() == 0;
        var isNull  = carrierCodes.FirstOrDefault() == null;

        if ( carrierCodes.ToList().Count == 0 )
        {
            //Don't try to access members of carrierCodes
        }

Exception:

My Exception

Updated Code:

     var code      = shipTEntity.CargoControlNum.Substring(0, 3);
        var profileId = shipTEntity.CompanyProfile.CompanyProfileID;

        var profiles = from cc in context.CompanyProfiles
                       where cc.ProfileTypeID == 9
                       select cc;

        var codesInProfiles = from p in profiles
                              where p.CompanyProfileCode == code
                              select p;

        var carrierCodes = from c in codesInProfiles
                        where c.CompanyProfileID == profileId
                        select c;

        //var carrierCodes = from cc in context.CompanyProfiles
        //                   where cc.ProfileTypeID == 9 &&
        //                         cc.CompanyProfileCode == shipTEntity.CargoControlNum.Substring( 0, 3 ) &&
        //                         cc.CompanyProfileID == shipTEntity.CompanyProfile.CompanyProfileID
        //                   select cc;

        if ( !profiles.Any() )
            return false;

        if ( !codesInProfiles.Any() )
            return false;

        if ( !carrierCodes.Any() )
            return false;

Answer: the problem was my profile Id was null. .Any() is a good substitute for Count() == 0 btw

  • 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-28T15:28:21+00:00Added an answer on May 28, 2026 at 3:28 pm

    As per my original comment, I would guess that one of the used related entities on shipTEntity is not pre-loaded. Probably it’s CompanyProfile in particular.

    To resolve this, when retrieving shipTEntity from the data context, you should set some DataLoadOptions.LoadsWith on the context to ensure that it’s related entities are also loaded from the context.

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

Sidebar

Related Questions

I have the following Linq to SQL query, in which I'm trying to do
Have the following (non-straightforward) T-SQL query, which i'm trying to convert to LINQ (to
Strange performance outcome, I have a LINQ to SQL query which uses several let
In my WPF app I have implemented LINQ to SQL query which populates an
I have a complex LINQ to SQL to query, which joins onto two tables
I have a LINQ TO SQL query which retrieves all the users along with
I have an LINQ-to-SQL (IQueryable) query, which I need to debug as it's horrendously
I have a controller which peforms a linq to sql query against a model
I have a LINQ query which is searching a SQL table of around 250,000
I have Linq to SQL query which return one field with Url and I

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.