• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1265
  • Last Modified:

Does SingleOrDefault return null on multiple results?

SingleOrDefault() is supposed to return an exception if there is more than one result returned: http://msdn.microsoft.com/en-us/library/bb342451.aspx.  I took the sql this method is sending to SQL Server and ran it in a query window.  I got back two results.  The method is returning null without throwing an exception.

Any ideas why the discrepancy?
0
brettr
Asked:
brettr
  • 3
  • 3
  • 2
  • +1
2 Solutions
 
Todd GerbertIT ConsultantCommented:
From http://msdn.microsoft.com/en-us/library/bb342451.aspx:

Remarks
The default value for reference and nullable types is nullNothingnullptra null reference (Nothing in Visual Basic)


Therefore,
string[] series = new String[] {};
object singleordef = series.SingleOrDefault();
// singleordef = null
Your set must be empty, and it's returning the default value for a reference type, which is null.
0
 
brettrAuthor Commented:
That doesn't address the question.  Here's the relevant text from the link, "this method throws an exception if there is more than one element in the sequence".
0
 
magicdlfCommented:
This is from Reflector:

public static TSource SingleOrDefault<TSource>(this IEnumerable<TSource> source)
{
    if (source == null)
    {
        throw Error.ArgumentNull("source");
    }
    IList<TSource> list = source as IList<TSource>;
    if (list != null)
    {
        switch (list.Count)
        {
            case 0:
                return default(TSource);

            case 1:
                return list[0];
        }
    }
    else
    {
        using (IEnumerator<TSource> enumerator = source.GetEnumerator())
        {
            if (!enumerator.MoveNext())
            {
                return default(TSource);
            }
            TSource current = enumerator.Current;
            if (!enumerator.MoveNext())
            {
                return current;
            }
        }
    }
    throw Error.MoreThanOneElement();
}

It should throw exception if there're more than one element.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Todd GerbertIT ConsultantCommented:
That's true: if you have zero elements, it returns the default value (or NULL if it's a series of reference/nullable types).

If you have 1 element, it returns that value.

If you have more than one element, you get an exception.

Since you're getting null, that means the first statement must be true - you have a series of reference/nullable types, so it's returning null.

If you post some of your code maybe I can help pin down why you're getting null.
0
 
Fernando SotoRetiredCommented:
Please show your code so we can give you a better reason why it is happening.
0
 
brettrAuthor Commented:
Which code do you want me to show?  I can show that single call but not sure if that is what you want.
0
 
Todd GerbertIT ConsultantCommented:
How you populate the SQL query, and how it's passed to SingleOrDefault.
0
 
brettrAuthor Commented:
The code used is below.  It returns two rows when I run its SQL but person is null after SingleOrDefault()
        private static Person getPerson(Expression<Func<Participant, bool>> query)
        {
            TheDataContext context = new TheDataContext();
            
                Person person = null;
                SetPersonLoadOptions(context);

                person = context.Persons.SingleOrDefault(query);
                
                return person;
        }

Open in new window

0
 
Fernando SotoRetiredCommented:
Please show code that is causing the error, the Linq query and also if you upload a test DB that you are working with. If you can not post the DB then script the tables to a file from Microsoft SQL Server Management Studio and post that.

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now