How to map the result of a stored procedure?

Nov 4, 2011 at 2:52 PM


I'm following this tutorial and I'm trying to use a stored procedure as I'll do in a real scenario, so mapping ComplexTypes...

I'm stucked at this point :

Tutorial :


 public INotifyCompleted SearchCustomers(string searchTerm, Action<IEnumerable<Customer>> onSuccess,
                                                Action<Exception> onFail)
            var query = Manager.Customers.Where(c => c.CompanyName.StartsWith(searchTerm));
            var op = query.ExecuteAsync();
            return op.OnComplete(onSuccess, onFail);

Mine :

public IdeaBlade.EntityModel.INotifyCompleted SearchCustomers(string searchTerm, Action<IEnumerable<ClienteRicercaResult>> onSuccess, Action<Exception> onFail)


       var query = Manager.IF_SP_GET_CLIENTI_FROM_PREFIXQuery(123, searchTerm, 0, 123456, 0);

      var op = query.ExecuteAsync();

       return op.OnComplete(onSuccess, onFail); //<- onComplete expects an Action OnSuccess ... not Action<IEnumerable<ClienteRicercaResult>> onSuccess



What can I do?




Nov 4, 2011 at 6:27 PM
Edited Nov 4, 2011 at 6:30 PM

The result of a stored procedure query is an untyped IEnumerable, not a typed IEnumerable<T> or in your case IEnumerable<ClienteRicercaResult>. You first have to cast the result. You can do it the following way: 

            return op.OnComplete(result => { if (onSuccess != null) onSuccess(result.Cast<ClienteRicercaResult>()); },

If that's too much clutter for you - it is for me, but I avoid stored procedures in general - you can download the latest source code. I've just checked in a new overload for OnComplete() that does this for you.

        /// <summary>Extension method to process the result of an asynchronous query operation.</summary>
        /// <param name="source">The EntityQueryOperation returned from an asynchronous query.</param>
        /// <param name="onSuccess">A callback to be called if the asynchronous query was successful.</param>
        /// <param name="onFail">A callback to be called if the asynchronous query failed.</param>
        /// <remarks>This overload automatically attempts to cast the results from IEnumerable to IEnumerable&lt;T&gt;"/></remarks>
        /// <returns>Returns the EntityQueryOperation passed to the method's source parameter.</returns>
        public static EntityQueryOperation OnComplete<T>(this EntityQueryOperation source,
                                                      Action<IEnumerable<T>> onSuccess, Action<Exception> onFail);

Nov 7, 2011 at 9:07 AM

Hello marcelgood,

I've done a similar thing meanwhile waiting for the answer....BTW I'll upgrade the source code to the latest version, so I've not to cast manually

Thanks again!