c# – 非静态方法需要一个目标

本文详细解析了在使用Linq进行数据库查询时遇到的TargetException异常,该异常源于非静态方法调用时目标对象为null的情况。通过具体代码示例,展示了如何定位问题并提供了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2019/1/25 5:21:10   在执行 controller[sys_logisticsexpress] 的 action[index] 时产生异常
参数 :[HttpMethod=GET,ContentType=]
异常 : System.Reflection.TargetException: 非静态方法需要一个目标。
   在 System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
   在 System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   在 System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
   在 System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.TryGetFieldOrPropertyValue(MemberExpression me, Object instance, Object& memberValue)
   在 System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.TryEvaluatePath(Expression expression, ConstantExpression& constantExpression)
   在 System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.EvaluateParameter(Object[] arguments)
   在 System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
   在 System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__6()
   在 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   在 System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
   在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   在 System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   在 System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   在 System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   在 System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   在 System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   在 Sixcom.Perssion.BLL.Base_BLL`1.GetALL(Expression`1 predicate)
   在 SixCom.SCM.Controllers.Sys.Sys_LogisticsExpressController.DropBind()

 

 

 

是因为 Linq 条件有 null 值导致的

 Sys_Dictionary_BLL.GetALL(m => m.DictType == (int)DictionaryType.License && m.Status == (int)Sixcom.Enum.Status.Save && m.OrgId == UserAdmin.Organization.Id).Select(m => new SelectListItem() { Text = m.Name, Value = m.Id.ToString() }).ToList();
 

这里的 UserAdmin 是null 值

所以在Linq 前 做判断

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值