none
Returning single item from ObjectResult RRS feed

  • Question

  • pretty new to edmx's  but i have a .Net4.0 Entity Data Model that calls a SQL storeprocedure to return an database entity 'booking'

    currently i have it as follows:-

      public static ObjectResult<Booking> GetBookingByBookingRef(string bookingRef)
    
      {
    
       using (var context = new OperateITNow_DEVEntities())
    
       {
    
        
    
        
    
        ObjectResult<Booking> booking;
    
    
    
        try
    
        {
    
         booking = context.GetBookingByBookingRef(bookingRef);
    
         
    
        }
    
        catch (Exception ex)
    
        {
    
         throw new Exception("GetBookingByBookingRef() - Error Message: " + ex.Message, ex.InnerException);
    
        }
    
        return booking;
    
       }
    
      }
    
    

    it will only ever return 1 booking, so rather than the function return ObjectResult<Booking> id prefer it to return this:-

      public static Booking GetBookingByBookingRef(string bookingRef)
    
      {
    
       using (var context = new OperateITNow_DEVEntities())
    
       {
    
        
    
        
    
        ObjectResult<Booking> booking;
    
    
    
        try
    
        {
    
         booking = context.GetBookingByBookingRef(bookingRef);
    
         
    
        }
    
        catch (Exception ex)
    
        {
    
         throw new Exception("GetBookingByBookingRef() - Error Message: " + ex.Message, ex.InnerException);
    
        }
    
        return booking;
    
       }
    
      }
    
    


    but i cant seem to find how to return the single booking rather than an ObjectResult containing the booking

    Any help is greatly appreciated.

    Cheers
    Craig


    Thursday, July 7, 2011 4:10 PM

Answers

  • Thats what i thinking but i didnt get any of those options, i only get following

    Dispose
    ElementType
    Equals
    GetEnumerator
    GetHasCode
    GetType
    ToString

    and now ive just realised why!! i didnt have the using System.Linq statement included!

    • Marked as answer by Craig G Tuesday, August 2, 2011 3:49 PM
    Thursday, July 7, 2011 4:34 PM

All replies

  •  Hi Craig;

     If the results could only have a max returned of one record you can use the SingleOrDefault method as shown below.

    booking = context.GetBookingByBookingRef(bookingRef).SingleOrDefault();

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, July 7, 2011 4:25 PM
  • Thats what i thinking but i didnt get any of those options, i only get following

    Dispose
    ElementType
    Equals
    GetEnumerator
    GetHasCode
    GetType
    ToString

    and now ive just realised why!! i didnt have the using System.Linq statement included!

    • Marked as answer by Craig G Tuesday, August 2, 2011 3:49 PM
    Thursday, July 7, 2011 4:34 PM
  • Hi Craig;

    After adding the namespace, is it working now?

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, July 7, 2011 5:21 PM
  • Hi Craig,

     

    Welcome to the forum.

    please add the namespace : System.Linq;

    The SingleOrDefault method is a static method in under the System.Linq namespace.

     

    System.Linq Source Code:

    using System.Linq

    {

          public static Class Enumerable

          {

                    public static TSouce SingleOrDefault(this ...)

          }

    }

     

    I hope that would be help you.

     

    Thanks to all the participators.



    David Peng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, July 11, 2011 2:55 AM
    Moderator