3

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?

Holland Schutte
  • 231
  • 2
  • 10

1 Answers1

8

Your call to .Single will throw this exception when the sequence is empty. If it is possible that no matches will be found, you should use .SingleOrDefault, which will return null if no matches exist.

It's very important to Understand the differences between Single, SingleOrDefault, First, and FirstOrDefault.

Here's a helpful visualization of these methods:

╔═════════════════╦═════════════╦═════════╦═════════════════════╗
║                 ║ [] (Empty)  ║ ["one"] ║ ["one", "two",...]  ║
╠═════════════════╬═════════════╬═════════╬═════════════════════╣
║ FirstOrDefault  ║ null        ║ "one"   ║ "one"               ║
║ First           ║ Exception   ║ "one"   ║ "one"               ║
║ SingleOrDefault ║ null        ║ "one"   ║ Exception           ║
║ Single          ║ Exception   ║ "one"   ║ Exception           ║
╚═════════════════╩═════════════╩═════════╩═════════════════════╝
╔═════════════════╦═════════════╦═════════╦═════════════════════╗
║ Error messages: ║ no elements ║         ║ multiple elements   ║
╚═════════════════╩═════════════╩═════════╩═════════════════════╝

As you can see, FirstOrDefault is the only one that won't throw an exception.

So, what it comes down to is this:
The ONLY reason to use First, SingleOrDefault, or Single, 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.

Scott Rippey
  • 15,614
  • 5
  • 70
  • 85
  • Thank you for responding - I changed the code to what you suggested, and I'm still receiving the same error. is there a chance I may be implementing my query improperly? – Holland Schutte Nov 13 '11 at 20:16
  • 1
    The ONLY time "Sequence contains no elements" error is thrown is when using `Single` or `First` ... it will not be thrown by `*OrDefault`. – Scott Rippey Nov 14 '11 at 18:03
  • @HollandSchutte I updated my answer with a better explanation of the different methods. Hopefully, that will help you to find the source of your issue. – Scott Rippey Nov 14 '11 at 20:16
  • Thank you, Scott. I will review as soon as possible. – Holland Schutte Nov 19 '11 at 13:51