接下来的L2S代码给了我这种非常奇怪的堆栈跟踪。如果我连接调试器并在此调试器的顶部设置断点,则问题似乎出在评估transaction.Count()上。谁能具体看到是什么原因造成的?请注意,“事务”表中没有属于该where子句的行。
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
完全失去了这一点。如果有人可以在这里提供任何帮助,将不胜感激!
最佳答案
您的子表之一可能是元凶。我刚遇到这个错误,这是由于将空对象传递给了辅助函数。不幸的是,您无法深入了解。
关于c# - Linq to SQL:System.Data.Linq.SqlClient.QueryConverter.VisitInvocation(InvocationExpression invoke)中的NullReferenceException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3012147/