Checking for null value in c# var with LINQ & Entity framework?

In your example entry will never be null What you think of as null is in fact an IEnumerable e. Email == entry. Email); if (entries.Any()) { // ... } If you know that there will be at most one entry, then you can also do: var entry = myDB.Entries.

Where(e => e. Email == entry. Email).SingleOrDefault(); if (entry!

= null) { // ... } This is closer to what you imagined, but will throw an exception if there is more than one matching entry.

In your example, entry will never be null. What you think of as null is in fact an IEnumerable with no items. If you want to check if there is at least one entry with your criteria, you normally do something like: var entries = myDB.Entries.

Where(e => e. Email == entry. Email); if (entries.Any()) { // ... } If you know that there will be at most one entry, then you can also do: var entry = myDB.Entries.

Where(e => e. Email == entry. Email).SingleOrDefault(); if (entry!

= null) { // ... } This is closer to what you imagined, but will throw an exception if there is more than one matching entry.

Thanks! Great help and insight as I forgot that I was using DBContext to check for entries. – yulun Mar 8 at 10:04 You could append .SingleOrDefault() to the query to get one result or null as well.

– Jason Goemaat Mar 8 at 10:06 @Jason: I already mention that... – Jon Mar 8 at 10:15.

Var" keywords make it possible to achieve any type based on the assignment at runtime, so when you query using "Where" the var entry becomes "IEnumerable" that is returned by Where, that's why you have to check for count.

Var entries = myDB.Entries. Where(e => e. Email == entry.

Var entry = myDB.Entries. Where(e => e. Email == entry.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions