以下的L2S代碼位給了我這個非常奇怪的堆棧跟蹤。如果我附加調試器並在此調試器上設置斷點,則看起來問題在於評估transactions.Count()。任何人都可以看到具體會導致這一點?請注意,Transaction表中沒有屬於此where子句的行。Linq to SQL:System.Data.Linq.SqlClient.QueryConverter.VisitInvocation中的NullReferenceException(調用表達式調用)
的L2S段:
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();
}
}
退貨的交易;
堆棧跟蹤:
[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
完全失去了在這一個。如果有人可以在這裏提供任何幫助,將不勝感激!
或'公司'。也許'公司'是空的。 – Steven 2010-06-10 08:20:06
啊,那可能是,我會給你一個鏡頭。從架構的角度來看,爲什麼它不會拋出異常,直到* LINQ查詢執行後的任何想法? – 2010-06-10 16:12:23
@Brad:在執行查詢時,它會拋出異常。在你調用Count之前它不會被執行(搜索LINQ和「延遲執行」以獲取更多信息) – 2010-06-10 16:15:45