tags:

views:

105

answers:

1

The following bit of L2S code is giving me this very odd stack trace. If I attach a debugger and set a breakpoint on top of this debugger, it looks like the issue lies in evaluating transactions.Count(). Can anyone see specifically what would be causing this? Note that there are not rows in the Transactions table that fall in to this where clause.

The L2S Snippet:

var transactions = (from t in DataContext.Transactions
                    where t.TransactionDate.Date == date.Date && company.Id == t.Customer.CompanyId
                    select t);

if (transactions.Count() > 0) // Exception thrown here (transactions.Count())
{
    foreach (var transaction in transactions)
    {
        transaction.Detach();
    }
}

return transactions;

The Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   System.Data.Linq.SqlClient.QueryConverter.VisitInvocation(InvocationExpression invoke) +471
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1370
   System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +30
   System.Data.Linq.SqlClient.QueryConverter.VisitBinary(BinaryExpression b) +27
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +449
   System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +30
   System.Data.Linq.SqlClient.QueryConverter.VisitBinary(BinaryExpression b) +40
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +449
   System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +30
   System.Data.Linq.SqlClient.QueryConverter.VisitWhere(Expression sequence, LambdaExpression predicate) +136
   System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) +4173
   System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) +70
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1025
   System.Data.Linq.SqlClient.QueryConverter.VisitAggregate(Expression sequence, LambdaExpression lambda, SqlNodeType aggType, Type returnType) +84
   System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) +6371
   System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) +70
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1025
   System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node) +111
   System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations) +114
   System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +132
   System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +23
   System.Linq.Queryable.Count(IQueryable`1 source) +243
   MyProject.Repositories.TransactionRepository.GetTransactionsOn(DateTime date, Company company) in D:\Build\MyProject\Source\MyProject\Repositories\TransactionRepository.cs:38
   MyProject.Web.Widgets.RunningSalesTotalWidget.GetView() in D:\Build\MyProject\Source\MyProject\Web\Widgets\RunningSalesTotalWidget.cs:26
   MyProject.Web.WidgetController.Render(ViewContext viewContext) in D:\Build\MyProject\Source\MyProject\Web\WidgetController.cs:38
   MyProject.Web.Helpers.WidgetExtensions.RenderWidget(HtmlHelper helper, Int32 id) in D:\Build\MyProject\Source\MyProject.Web\Helpers\WidgetExtensions.cs:20
   ASP.views_home_index_aspx.__RenderContent2(HtmlTextWriter __w, Control parameterContainer) in d:\Build\MyProject\Source\MyProject.Web\Views\Home\Index.aspx:9
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +109
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in d:\Build\MyProject\Source\MyProject.Web\Views\Shared\Site.Master:30
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +109
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +56
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3060

Completely lost on this one. If anyone could provide any help here it would be greatly appreciated!

A: 

May it be that t.Customer can be null? You could try something like this and see if that works without throwing the exception (assuming that CompanyId is an int):

var transactions = (from t in DataContext.Transactions
                    where t.TransactionDate.Date == date.Date 
                        && company.Id == (t.Customer != null ? t.Customer.CompanyId : -1)
                    select t);
Fredrik Mörk
Or `company`. Perhaps `company` is null.
Steven
Ahh, that could be, I'll give that a shot. From an architectural standpoint, any idea why it doesn't throw the exception till *after* the LINQ query executes?
Brad Heller
@Brad: it *does* throw the exception while the query is executed. It is not executed until you call `Count` (search for LINQ and "deferred execution" for more info on that)
Fredrik Mörk