I have a rather ugly service job that runs through a legacy database and compares it to our production database:
if (vendorContract.Item.Where(x => x.ItemNumber == contractItem.Item_Number) != null) {
var oldDbContractItem = vendorContract.Item.Where(x => x.ItemNumber == contractItem.Item_Number).First();
// check to see if there were changes
if (oldDbContractItem.DateStamp != vendorContractItem.Date_Stamp)
{
oldDbContractItem.Update(vendorContractItem);
}
}
I will get an error on var oldDbContratItem, “Sequence contains no elements”, yet I just did a != null check. This must be simple, what’s going on?
This is the test against
nullyou are doing:That will always be true; That will always return at least an empty sequence… never a
null.You might be meaning to test that its length is greater than 0?
There’s an easier way, though, IMO. Call
FirstOrDefault()instead ofFirst()and leave out the pre-test completely. Then instead, test if the result ofFirstOrDefault()isnull.