执行LINQ查询时出现异常

时间:2010-12-03 06:20:48

标签: c# linq exception-handling

我在ASP.NET MVC2中开发它

获得例外:

Object reference not set to an instance of an object

System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="Anonymously Hosted DynamicMethods Assembly"
  StackTrace:
       at lambda_method(ExecutionScope , TrusteeMaster )
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
       at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
       at LexEyeSystem.Controllers.TrusteeMasterController.JsonContractCollection(Int32 page, Int32 rows, String sidx, String sord) in D:\ParallelMinds\Projects\LexEye\Controllers\TrusteeMasterController.cs:line 560
       at lambda_method(ExecutionScope , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException: 

我正在尝试显示jqgrid,为此我必须编写此JSON返回操作,其中包含我的此Linq查询。相同的查询在其他页面上正常运行:

var jsonData = new 
{ 
    total = totalPages, 
    page = page, 
    records = totalRecords, 
    rows = ( 
          from s in sortedList 
          select new 
          { 
              cell = new string[] {                         
                "TrustContract/Edit/"+ s.TrusteeId.ToString(),                           
                s.FullName,
                s.OtherName.ToString(),
                s.FatherName,
                s.TrusteeGender,
                s.ResidentialAddress, 
                s.CommunicationAddress,
                s.Occupation,
                s.Nationality,
                s.DateOfBirth.Value.ToShortDateString(),
                s.PlaceOfBirth,
                s.TelephoneNumber,
                s.FacsimileNumber,
                s.MobileNumbers,
                s.EmailId,
                s.ElectionIdCardNumber,
                s.PANCardNumber,
                s.TaxRegistrationNumber,
                s.Qualification,
                s.ExperienceInYears.ToString(),
                s.Resume.ToString(), 
                "DisclousureInterestDetails/Index/"+ s.TrusteeId.ToString(),//+"?trusteeId="+s.TrusteeId.ToString(), 
                "ForeignDetail/Create/"+ s.TrusteeId.ToString(),
                "InterestedOtherCompanyInformation/Index/"+ s.TrusteeId.ToString(), 
                "RelativesDetails/Index/"+ s.TrusteeId.ToString() 

            }
          }).ToArray()
};

但为什么不应该在这里跑?

编辑: LINQ查询获取sortedList:

var sortedList = objTrusteeMasterList
                    .AsQueryable()
                    .OrderBy(orderBy) // Uses System.Linq.Dynamic library for sorting
                    .Skip(pageIndex * pageSize)
                    .Take(pageSize);

2 个答案:

答案 0 :(得分:1)

检查from s in sorteList中的sortedList是否为NULL。

我认为当LINQ在MoveNext上调用sortedList方法时抛出异常。

答案 1 :(得分:0)

我遇到了同样的问题。如果我在

之后调用ToArray()方法
from s in sortedList 
          select new ...

我也有例外。问题是对 ToString()的调用。如果元素为null,则会失败。