Adrian Chrostowski
Adrian Chrostowski

Reputation: 434

Sequence contains more than one element - while it should contain more than one

I know the topic of "Sequence contains more than one element" was covered here before but I couldn't find anything that would apply to my case.
Here's the code:

var mailTo = db.DRAFT_DLs
    .Where(dd => dd.MX_DL == argMailTo)
    .Select(dd => new MailAddress(dd.EMAIL))
    .ToList();

string mailCc = db.DRAFT_DLs
    .Where(dd => dd.MX_DL == "ALL")
    .Select(dd => dd.EMAIL)
    .SingleOrDefault() ?? "";

// ...

mailTo.ForEach(rcpt => mail.To.Add(rcpt));
mail.CC.Add(mailCc);

Complete error message:

Message :System.InvalidOperationException: Sequence contains more than one element
   at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
   at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
   at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)
   at IncidentEmailEngine.Program.Main(String[] args) in c:\Dane\VS\IncidentEmailEngine\IncidentEmailEngine\Program.cs:line 94<br/>
StackTrace :   at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
   at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
   at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)
   at IncidentEmailEngine.Program.Main(String[] args) in c:\Dane\VS\IncidentEmailEngine\IncidentEmailEngine\Program.cs:line 94

And mentioned exception is thrown on the first line. The problem is that it's a list and therefore it should and contains more than one element.

Thanks for your input on that.
Resolution:
Error was in line 95, as the result of the query had more than one record. Rewriting it to:

var mailTo = db.DRAFT_DLs
    .Where(dd => dd.MX_DL == argMailTo)
    .Select(dd => new MailAddress(dd.EMAIL))
    .ToList();

var mailCc = db.DRAFT_DLs
    .Where(dd => dd.MX_DL == "ALL")
    .Select(dd => new MailAddress(dd.EMAIL))
    .ToList();

// ...

mailTo.ForEach(rcpt => mail.To.Add(rcpt));
mailCc.ForEach(rcpt => mail.CC.Add(rcpt));

solved the issue. Still not known is the reason why error was thrown on a different line tough.

Upvotes: 1

Views: 6400

Answers (2)

Richard Ev
Richard Ev

Reputation: 54147

The documentation for SingleOrDefault tells us that:

InvalidOperationException (is thrown when) The input sequence contains more than one element.

In your case, this means that in db.DRAFT_DLs you have more than one record where dd.MX_DL equals "ALL".

You should be able to verify this by querying the DRAFT_DL table in your database.

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460340

It's not thrown on the first but on the second line:

string mailCc = db.DRAFT_DLs
   .Where(dd => dd.MX_DL == "ALL")
   .Select(dd => dd.EMAIL)
   .SingleOrDefault() ?? "";

If you use Enumerable.SingleOrDefault(or Single) you say that zero or one records are ok but more than one is exceptional. That's why you get the exception. With Single zero is also exceptional. Maybe you want to use FirstOrDefault:

string mailCc = db.DRAFT_DLs
   .Where(dd => dd.MX_DL == "ALL")
   .Select(dd => dd.EMAIL)
   .FirstOrDefault() ?? "";

Upvotes: 3

Related Questions