I’m quite new to LINQ & Entity framework as well as the var keyword in c# so please pardon me if this sounds like a ‘newbie’ question.
I have problems checking for null values after doing something like this:
var entry = myDB.Entries.Where(e => e.Email == entry.Email);
Even when the email does not exist in the database, entry does not equate to null.
So instead of if (entry == null) i had to do if (entry.Count() < 1) to check for existing Entry before i execute my next batch of statements. Is there any reason why the variable wouldn’t be considered null?
In your example,
entrywill never benull. What you think of asnullis in fact anIEnumerable<Entry>with no items.If you want to check if there is at least one entry with your criteria, you normally do something like:
If you know that there will be at most one entry, then you can also do:
This is closer to what you imagined, but will throw an exception if there is more than one matching entry.