1

I am getting non static method requires a target error, and when I googled it I have seen posts to check null values and in my data I have all the values declared as not null and not sure why I am getting this error. Please help.

MyCode:

I am getting total results by doing a call to Db

    var result = _retailerStatsRepository.GetAllRetailersForManufacturerCountryAndCategorySelectedDates(manufacturerRow.Id,
                                                                                        countryRow.Id,
                                                                                        categoryRow.Id,
                                                                                        cumLeads.StartDate,
                                                                                        cumLeads.EndDate);

Note:

I am trying to use result for my further queries using 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);

I am getting error so I tries below

Edit

 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);
                    }

But still getting same error and checked inn my DB and I have no data only for few days between my startdate and end date for the category I have selected.

StackTrace :

at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target) at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index) at System.Data.Objects.ELinq.QueryParameterExpression.TryGetFieldOrPropertyValue(MemberExpression me, Object instance, Object& memberValue) at System.Data.Objects.ELinq.QueryParameterExpression.TryEvaluatePath(Expression expression, ConstantExpression& constantExpression) at System.Data.Objects.ELinq.QueryParameterExpression.EvaluateParameter(Object[] arguments) at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable1 forMergeOption) at System.Data.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption) at System.Data.Objects.ObjectQuery1.System.Collections.Generic.IEnumerable.GetEnumerator() at System.Linq.Enumerable.Single[TSource](IEnumerable1 source) at System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__3[TResult](IEnumerable1 sequence) at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable1 query, Expression queryRoot) at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression) at System.Linq.Queryable.Sum[TSource](IQueryable1 source, Expression1 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, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 continuation)

As I am new to Linq , please help me if I am doing anything wrong.

Liam
  • 27,717
  • 28
  • 128
  • 190
62071072SP
  • 1,963
  • 2
  • 19
  • 38
  • Is this a *compiler* error? or an exception at runtime? Which part *specifically* reports the error? If this is an exception: what is the stacktrace? – Marc Gravell Nov 12 '13 at 10:57
  • When they're talking about `null` in this context, it means you are passing a null object into your query. It's not about your database columns being nullable. I see a few potential nulls: `manufacturerRow`, `countryRow`, `cumLeads`, `localStart` and `localEnd`. – CodeCaster Nov 12 '13 at 10:59
  • For @Dennis http://stackoverflow.com/questions/13717355/non-static-method-requires-a-target try and look here – David Pilkington Nov 12 '13 at 11:01
  • Surely your first instinct should be to post the actual error instead of making us guess? – Jeroen Vannevel Nov 12 '13 at 11:01

1 Answers1

1

You can do something like this and also check for null values

For example I am giving below for 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));
                    }
Achieve Solution
  • 190
  • 1
  • 11