How to handle ObjectResult in Entity Framework 4

前端 未结 1 1828
Happy的楠姐
Happy的楠姐 2020-12-08 13:17

In Entity Framework 4, I\'m facing a problem when I use function import to a stored procedure and then using as a scalar value. It generates the code below:



        
相关标签:
1条回答
  • 2020-12-08 13:59

    Notice that the return type of GetTopEmployee is an ObjectResult<Nullable<int>>. ObjectResult<T> implements IEnumerable<T> so your method is capable of returning more than one Nullable<int>.

    I assume you are trying to get some form of Id for the employee. If you are absolutely sure you are only going to get one result you could use the following code to get that Id.

      var topId = db.GetTopEmployee().FirstOrDefault();
    

    topId could be null. You will have to use topId.Value or var result = topId ?? -1; to get an int result.

      NorthwindEntities db = new NorthwindEntities();
      int j = db.GetTopEmployee().FirstOrDefault() ?? -1;
    
    0 讨论(0)
提交回复
热议问题