获取非静态方法需要目标错误

时间:2013-11-12 10:53:17

标签: c# asp.net-mvc linq entity-framework

我得到非静态方法需要一个目标错误,当我用Google搜索它时,我看到了检查空值的帖子,在我的数据中,我将所有值声明为非null并且不确定为什么我收到此错误。请帮忙。

mycode的:

通过拨打Db

获得总结果
    var result = _retailerStatsRepository.GetAllRetailersForManufacturerCountryAndCategorySelectedDates(manufacturerRow.Id,
                                                                                        countryRow.Id,
                                                                                        categoryRow.Id,
                                                                                        cumLeads.StartDate,
                                                                                        cumLeads.EndDate);

注意:

我正在尝试使用结果进行使用Linq的进一步查询

   retailerWeeklyClickCount = result.Where(
                       i =>
                       i.Date >= localStart && i.Date <= localEnd && i.RetailerId == retailer.Id &&
                       i.ManufacturerId == manufacturerRow.Id
                       && i.CountryId == countryRow.Id && i.CategoryId == categoryRow.Id).Sum(i => i.WidgetClicks);

我收到错误,所以我尝试下面

编辑

 var retailerWeeklyClicks = result.Where(
                        i =>
                        i.Date >= localStart && i.Date <= localEnd && i.RetailerId == retailer.Id &&
                        i.ManufacturerId == manufacturerRow.Id
                        && i.CountryId == countryRow.Id && i.CategoryId == categoryRow.Id);

 if(retailerWeeklyClicks!=null)
                    {
                        retailerWeeklyClickCount = retailerWeeklyClicks.Sum(i => i.WidgetClicks);
                    }

但是仍然得到相同的错误并检查了我的数据库,并且在我选择的类别的开始日期和结束日期之间的几天内我没有数据。

StackTrace:

  

at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)   在System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object   obj,BindingFlags invokeAttr,Binder binder,Object []参数,   文化信息文化)   System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags   invokeAttr,Binder binder,Object []参数,CultureInfo文化)
  在System.Reflection.RuntimePropertyInfo.GetValue(Object obj,   BindingFlags invokeAttr,Binder binder,Object [] index,CultureInfo   system.Reflection.RuntimePropertyInfo.GetValue(Object。)   obj,Object [] index)at   System.Data.Objects.ELinq.QueryParameterExpression.TryGetFieldOrPropertyValue(MemberExpression   我,对象实例,对象&amp; memberValue)at   System.Data.Objects.ELinq.QueryParameterExpression.TryEvaluatePath(表达式   表达式,ConstantExpression&amp; constantExpression)   System.Data.Objects.ELinq.QueryParameterExpression.EvaluateParameter(对象[]   争论   System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(可空1 forMergeOption) at System.Data.Objects.ObjectQuery 1.GetResults(可空1 forMergeOption) at System.Data.Objects.ObjectQuery 1.System.Collections.Generic.IEnumerable.GetEnumerator()   在System.Linq.Enumerable.Single [TSource](IEnumerable 1 source) at System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__3[TResult](IEnumerable 1   序列)   System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle [TResult](IEnumerable的1 query, Expression queryRoot) at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression) at System.Linq.Queryable.Sum[TSource](IQueryable 1   来源,表达式1 selector) at reporting.e_tale.co.uk.Controllers.ReportsController.CumLeadsParameters(CumLeadsReport cumLeads) in d:\e-tale-core-development\trunk\reporting.e-tale.co.uk\Controllers\ReportsController.cs:line 492 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 2个参数)at   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext   controllerContext,ActionDescriptor actionDescriptor,IDictionary 2 parameters) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func 1 continuation)

因为我是Linq的新手,如果我做错了什么,请帮助我。

1 个答案:

答案 0 :(得分:1)

您可以执行类似的操作并检查空值

例如我在下面给出了RetailerId:

object resultOfSum = result.Where(
                       i =>
                       i.Date >= localStart && i.Date <= localEnd && i.RetailerId !=null ? i.RetailerId== retailer.Id:i.RetailerId==null &&
                       i.ManufacturerId == manufacturerRow.Id
                       && i.CountryId == countryRow.Id && i.CategoryId == categoryRow.Id).Sum(i=>(int?)(i.WidgetClicks))??0;
                    if(resultOfSum!=null)
                    {
                        retailerWeeklyClickCount = (Convert.ToInt32(resultOfSum));
                    }