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:

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
As per my original comment, I would guess that one of the used related entities on
shipTEntityis not pre-loaded. Probably it’sCompanyProfilein particular.To resolve this, when retrieving
shipTEntityfrom 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.