I’m having a really hard time debugging some LINQ to SQL code because there is no call stack and I have no idea where the error is occurring. It doesn’t seem to be a SQL error – it’s in C#, but through searching around and everything else, I can’t find any way to figure out which calculation is giving the error.
The source code is below and you can see the error that is thrown when I try to insert some new rows. The call stack is empty and the exception contains no information about what went wrong.
var groups =
from record in startTimes
group record by record.startTime
into g
select new
{
startTime = g.Key.GetValueOrDefault(),
totalTasks = g.Count(),
totalTime = g.Max(o => o.record.timeInSession).GetValueOrDefault(),
minDwell = g.Min(o => o.record.dwellTime).GetValueOrDefault(),
maxDwell = g.Max(o => o.record.dwellTime).GetValueOrDefault(),
avgDwell = g.Average(o => o.record.dwellTime).GetValueOrDefault(),
stdevDwell = g.Select(o => Convert.ToDouble(o.record.dwellTime)).StdDev(),
correct80 = g.Sum( o => o.record.correct80.GetValueOrDefault() ? 1 : 0),
wrong80 = g.Sum(o => o.record.wrong80.GetValueOrDefault() ? 1 : 0)
};
var statistics = groups.AsEnumerable().Select(
g => new e_activeSession()
{
workerId = wcopy,
startTime = g.startTime,
totalTasks = g.totalTasks,
totalTime = g.totalTime,
minDwell = g.minDwell,
maxDwell = g.maxDwell,
avgDwell = g.avgDwell,
stdevDwell = g.stdevDwell,
total80 = g.correct80 + g.wrong80,
correct80 = g.correct80,
percent80 = g.correct80 / (g.correct80 + g.wrong80)
}
);
// Put these rows into the table
_gzClasses.e_activeSessions.InsertAllOnSubmit(statistics);
_gzClasses.SubmitChanges();

Here’s the stack trace for the exception. If anyone can decipher it, please tell me, because I have no clue how to read these anonymous types…
at System.Data.SqlClient.SqlBuffer.get_Double()
at System.Data.SqlClient.SqlDataReader.GetDouble(Int32 i)
at Read_<>f__AnonymousType2`9(ObjectMaterializer`1 )
at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at System.Data.Linq.Table`1.InsertAllOnSubmit[TSubEntity](IEnumerable`1 entities)
Also, I’ve run this query in LINQPad and got the same error and stack trace. Finally, here’s the database query generated by LINQ to SQL that was executed right before the error. I don’t see any double conversions in it at all.
SELECT COALESCE([t5].[value22],'1/1/0001 12:00:00 AM') AS [startTime], [t5].[value] AS [totalTasks], COALESCE([t5].[value2],0) AS [totalTime], COALESCE([t5].[value3],0) AS [minDwell], COALESCE([t5].[value4],0) AS [maxDwell], COALESCE([t5].[value5],0) AS [avgDwell], [t5].[value6] AS [correct80], [t5].[value7] AS [wrong80], [t5].[value22]
FROM (
SELECT COUNT(*) AS [value], MAX([t4].[timeInSession]) AS [value2], MIN([t4].[dwellTime]) AS [value3], MAX([t4].[dwellTime]) AS [value4], AVG([t4].[dwellTime]) AS [value5], SUM([t4].[value3]) AS [value6], SUM([t4].[value]) AS [value7], [t4].[value2] AS [value22]
FROM (
SELECT
(CASE
WHEN (COALESCE([t3].[wrong80],0)) = 1 THEN @p4
ELSE @p5
END) AS [value], [t3].[workerID], [t3].[value2], [t3].[timeInSession], [t3].[dwellTime], [t3].[value] AS [value3]
FROM (
SELECT
(CASE
WHEN (COALESCE([t2].[correct80],0)) = 1 THEN @p2
ELSE @p3
END) AS [value], [t2].[wrong80], [t2].[workerID], [t2].[value] AS [value2], [t2].[timeInSession], [t2].[dwellTime]
FROM (
SELECT (
SELECT MAX([t1].[timeStamp])
FROM [dbo].[workerLog] AS [t1]
WHERE ([t1].[dwellTime] IS NULL) AND ([t1].[timeInSession] = @p0) AND ([t1].[workerID] = @p1) AND ([t1].[timeStamp] <= [t0].[timeStamp])
) AS [value], [t0].[workerID], [t0].[dwellTime], [t0].[timeInSession], [t0].[correct80], [t0].[wrong80]
FROM [dbo].[workerLog] AS [t0]
) AS [t2]
) AS [t3]
) AS [t4]
WHERE [t4].[workerID] = @p6
GROUP BY [t4].[value2]
) AS [t5]
Let’s read that call stack:
InsertAllOnSubmitis the top of the stack. It calledToList, which called List’s “ctor” which means constructor.That List constructor enumerates its parameter (a deferred query), causing the query to be resolved.
During the enumeration in the List constructor, we try to move to the next element in the query (
WhereSelectEnumerableIterator.MoveNext) – this is an enumerator created by your call to Select after AsEnumerable. In order to move to the new element, what we actually need to do is drill deeper into the query (ObjectReader.MoveNext), which is going to create some object from the database results.At this point, I’d like to point out that you didn’t say the type for
startTimes. I’m guessingstartTimesis a LinqToSql querySo, that database query was turned into a DataReader (not captured on the stack), and we are extracting a row from the DataReader (
Read_<>f__AnonymousType29(ObjectMaterializer1 )) so we can turn that row into the .net instance that is the result of the query.In the process of extracting values from the row, there’s a double value involved, so we try to extract that (
.GetDouble(Int32 i)and.get_Double()), . Now, we know that a DataReader can think of its values as anobject[](reference SqlDataReader.GetValues). It seems reasonable that GetDouble might be implemented something like this:So, the object in the data reader’s array doesn’t cast to double. The most likely posibility is that the object is null!
It is also remotely possible that some other type is present (string or byte[] or whatever), but if you used LinqToSql’s designer to make a dbml file, and the tables in the database still agree with that dbml, this is unlikely.
I see that Convert.ToDouble is not in the call stack. This must have been done before – in the database. Convert.ToDouble is -never actually called-… instead it is translated to sql and run there. The rules for type conversion are different in the database.