I know I’m doing something wrong here, I just don’t know what as I’m not very sure what the issue is. Here’s the code which is called:
The call
System.Linq.Expressions.Expression<Func<AccountDataModel, bool>> deleg =
(m => m.Email == model.Email);
AccountDataModel query = database.FindBy(deleg);
Where the call leads to
public T FindBy(Expression<Func<T, bool>> expression)
{
return FilterBy(expression).Single();
}
public IQueryable<T> FilterBy(Expression<Func<T, bool>> expression)
{
return All().Where(expression).AsQueryable();
}
public IQueryable<T> All()
{
return (from data in _session.Query<T>()
select data);
}
The exception thrown
Sequence contains no elements
Ze details
Basically, what I’m trying to test right now is a registration module on my website which is supposed to search for an email which has been provided to see if it exists. I have an encrypted email address hidden in the database (and, yes, the email in the model has been encrypted as well) which is supposed to match up with the registration email provided. The problem is that no results are being returned.
What exactly am I doing wrong here?
Your call to
.Singlewill throw this exception when the sequence is empty. If it is possible that no matches will be found, you should use.SingleOrDefault, which will returnnullif no matches exist.It’s very important to Understand the differences between
Single,SingleOrDefault,First, andFirstOrDefault.Here’s a helpful visualization of these methods:
As you can see,
FirstOrDefaultis the only one that won’t throw an exception.So, what it comes down to is this:
The ONLY reason to use
First,SingleOrDefault, orSingle, is so that an Exception will be thrown if your results are not as you expect!This will give you a better debugging experience, and gives your code better semantic meaning.