IEnumerable.Any()和.Count()引发TargetInvocationException,即使我

本文关键字:引发 Any Count IEnumerable TargetInvocationException | 更新日期: 2023-09-27 18:07:17

背景

我正在使用LINQ to SQL从数据库中获取信息。我最近更改了查询以使用数据库的更新版本,在该版本中,我将字符串列切换为位列。然而,当我查看数据库的旧版本时,它会崩溃。

与其崩溃,我希望程序打开时有一个空列表,这样用户下次就可以更改设置,在正确的位置查找。如果它在打开时崩溃,他们无法更改设置。

代码

LINQ查询运行良好,但如果我尝试使用.Any().Count()检查它是否为空,或者尝试将其强制转换为列表,它会崩溃并引发TargetInvocationException异常,内部异常为NullReferenceException。但是,它不是null。

IEnumerable<Airplane> propPlanes = _dataContext.Airplanes.Where(p =>
    p != null &&
    p.IsPropeller == true // used to be p.IsPropeller == "YES" 
);
List<Airplane> propPlanesList = new List<Airplane>();
if (propPlanes != null && _dataContext.Airplanes != null)  // expression is always true
{
    if (propPlanes.Any())                     // throws TargetInvocationException 
    {
        propPlanesList = propPlanes.ToList(); // would throwTargetInvocationException
    }
}
return propPlanesList ;

我如何检查查询是否没有任何结果?

TargetInvocationException:的堆栈跟踪

   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Activator.CreateInstance(Type type, Object[] args)
   at System.Xaml.Schema.SafeReflectionInvoker.CreateInstanceCritical(Type type, Object[] arguments)
   at System.Xaml.Schema.XamlTypeInvoker.CreateInstance(Object[] arguments)
   at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstance(XamlType xamlType, Object[] args)
   at System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx)
   at System.Xaml.XamlObjectWriter.WriteStartMember(XamlMember property)
   at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector)
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc)
   at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties)
   at System.Windows.Application.DoStartup()
   at System.Windows.Application.<.ctor>b__1(Object unused)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run()
   at ClaytonSystem.App.Main() in ''pcppnasw02'users$'cclayto'My Documents'Work'GitHub'c'V2'WPFDataGrid'obj'Debug'App.g.cs:line 0
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

内部异常(NullReferenceException(的堆栈跟踪:

   at System.Data.Linq.SqlClient.QueryConverter.VisitInvocation(InvocationExpression invoke)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp)
   at System.Data.Linq.SqlClient.QueryConverter.VisitBinary(BinaryExpression b)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp)
   at System.Data.Linq.SqlClient.QueryConverter.VisitBinary(BinaryExpression b)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp)
   at System.Data.Linq.SqlClient.QueryConverter.VisitBinary(BinaryExpression b)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.VisitWhere(Expression sequence, LambdaExpression predicate)
   at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node)
   at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.DataQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source)
   at ClaytonSystem.MainWindow.GetCheckTasks() in ''pcppnasw02'users$'cclayto'My Documents'Work'GitHub'c'V2'WPFDataGrid'Window1.xaml.cs:line 611
   at ClaytonSystem.MainWindow.ListCheckTasks() in ''pcppnasw02'users$'cclayto'My Documents'Work'GitHub'c'V2'WPFDataGrid'Window1.xaml.cs:line 751
   at ClaytonSystem.MainWindow..ctor() in ''pcppnasw02'users$'cclayto'My Documents'Work'GitHub'c'V2'WPFDataGrid'Window1.xaml.cs:line 129

IEnumerable.Any()和.Count()引发TargetInvocationException,即使我

我怀疑这个问题是由于您已经将模型中的string引用类型更改为bool值类型,并且数据库尚未更新。Linq-To-Sql期望IsPropeller属性始终具有其值,并且当它无法将检索到的"YES"/"NO"string转换为bool时,属性会爆炸。

当Linq to Sql尝试创建实体实例时,似乎出现了错误。

虽然我不知道这个问题的真正答案,因为你使用的Linq to Sql并不是预期的,但你可以尝试以下方法:

1( 删除p != null检查,因为这无法直接转换为SQL,可能会导致读取所有数据记录并在客户端进行筛选;

2( 在IsPropeller属性上添加额外的null检查以欺骗Linq to Sql执行null检查:

p.IsPropeller != null && p.IsPropeller == true

3( 如果这没有帮助,请将IsPropeller类型更改为bool?,并添加额外的null检查:

p.IsPropeller.HasValue && p.IsPropeller == true

4( 尝试在比较前通过转换值来确定值,这样它既适用于bit又适用于varchar:

SqlFunctions.StringConvert((double)p.IsPropeller == "true"