• 下个路口   2016/4/2 10:45:00
  • The null value cannot be assigned to a member with type System.Double which is a non-nullable value
  • 关键字: Linq 匿名 non-nullable
  • 我在执行如下Linq语句时:
    MyLanqiuDataContext dataContext = new MyLanqiuDataContext();
    string sql = string.Format("SELECT AVG(datediff(year,birthday,getdate())) AS AVGAge, avg([Height]) as AVGHight," +
    " avg([weight]) as AVGWeight FROM [Com.ABC.MyLanqiu].[dbo].[UserInfo] where UserID in ( SELECT [UserID] " +
    " FROM [Com.ABC.MyLanqiu].[dbo].[TeamUserRelation] where TeamID={0})", teamID);
    return dataContext.ExecuteQuery<TeamStatisticsInfo>(sql).FirstOrDefault();
    出现如下错误:

    The null value cannot be assigned to a member with type System.Double which is a non-nullable value type.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: The null value cannot be assigned to a member with type System.Double which is a non-nullable value type.

    Source Error:

    Line 572:    " avg([weight]) as AVGWeight FROM [Com.ABC.MyLanqiu].[dbo].[UserInfo] where UserID in ( SELECT [UserID] " +
    Line 573:" FROM [Com.ABC.MyLanqiu].[dbo].[TeamUserRelation] where TeamID={0})", teamID);
    Line 574:            return dataContext.ExecuteQuery<TeamStatisticsInfo>(sql).FirstOrDefault();
    Line 575:        }
    Line 576:        public static void FocusTeam(int teamID, int userID)

    Source File: E:\Practice\Com.ABC.Mylanqiu\Com.ABC.Mylanqiu\DAL\TeamDAL.cs    Line: 574

    Stack Trace:

    [InvalidOperationException: The null value cannot be assigned to a member with type System.Double which is a non-nullable value type.]
       Read_TeamStatisticsInfo(ObjectMaterializer`1 ) +6952
       System.Data.Linq.SqlClient.ObjectReader`2.MoveNext() +32
       System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source) +164
       Com.ABC.Mylanqiu.DAL.TeamDAL.GetTeamStatisticsInfo(Int32 teamID) in E:\Practice\Com.ABC.Mylanqiu\Com.ABC.Mylanqiu\DAL\TeamDAL.cs:574
       Com.ABC.Mylanqiu.BLL.TeamBLL.GetTeamStatisticsInfo(Int32 teamID) in E:\Practice\Com.ABC.Mylanqiu\Com.ABC.Mylanqiu\BLL\TeamBLL.cs:543
       Com.ABC.Mylanqiu.Controllers.LeagueController.PK(String id) in E:\Practice\Com.ABC.Mylanqiu\Com.ABC.Mylanqiu\Controllers\LeagueController.cs:511
       lambda_method(Closure , ControllerBase , Object[] ) +180
       System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
       System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +214
       System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
       System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
       System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +253
       System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +21
       System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
       System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +324
       System.Web.Mvc.Controller.ExecuteCore() +106
       System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +91
       System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
       System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +34
       System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +19
       System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10
       System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
       System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +48
       System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
       System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
       System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
       System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514928
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
    

大家的回答
    Linda  回复时间: 2016/4/2 10:54:00
    你声明的TeamStatisticsInfo这个对象,把里面的AVGAge,AVGHight这些数值型的对象声明成可空类型的即可,比如 public double? AVGHight { get; set; }
    下个路口  回复时间: 2016/4/2 10:46:00
    谁知道该如何解决这种问题?
页数: 1 - 1 (共2 条内容)
Prev
[1]
Next
  •   我来回答
  • 注册用户登录后才能发表评论,请 登录注册
  • 请您注意

    ·自觉遵守:爱国、守法、自律、真实、文明的原则

    ·尊重网上道德,遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他各项有关法律法规

    ·严禁发表危害国家安全,破坏民族团结、国家宗教政策和社会稳定,含侮辱、诽谤、教唆、淫秽等内容的作品

    ·承担一切因您的行为而直接或间接导致的民事或刑事法律责任

    ·您在编程中国社区新闻评论发表的作品,本网站有权在网站内保留、转载、引用或者删除

    ·参与本评论即表明您已经阅读并接受上述条款