Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1129
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 SotoCommented:
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 SotoCommented:
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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