none
Convert ObjectResult<Type> to List<Type> RRS feed

  • Question

  • My ExecuteFunction gives me a ObjectResult<Type> where Type is an entity.

    How do I convert it to a List<Type>?

    I can do

    ObjectResult<Type> resultList = <<Get it from ExecuteFunction>>

    List<Type> list = new List<Type>();

    foreach (Type t in ResultList)

    list.Add(t)

    I do not want this loop. Please assist.

    Tuesday, June 12, 2012 8:57 PM

Answers

  • On 6/12/2012 4:57 PM, PrivatePyle1982 wrote:
    > My ExecuteFunction gives me a ObjectResult<Type> where Type is an entity.
    >
    > How do I convert it to a List<Type>?
    >
    > I can do
    >
    > ObjectResult<Type> resultList = <<Get it from ExecuteFunction>>
    >
    > List<Type> list = new List<Type>();
    >
    > foreach (Type t in ResultList)
    >
    > list.Add(t)
    >
    > I do not want this loop. Please assist.
    >
     
    var list = new List<Type>;
     
    list = (from a in resultlist select a).Tolist();    // Linq query
     
    Wednesday, June 13, 2012 3:36 AM

All replies

  • Hi PrivatePyle1982;

    Lets assume that your object context is called ctx and you stored procedure in the model is called Customers_By_City and it takes one parameter called param1 with the value of London and the entity that it returns is a collection of Customer then your statement should look like this :

    List<Customer> cust = ctx.ExecuteFunction<Customer>( "Customers_By_City", new ObjectParameter( "param1", "London" ) ).ToList( );

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, June 13, 2012 3:36 AM
  • On 6/12/2012 4:57 PM, PrivatePyle1982 wrote:
    > My ExecuteFunction gives me a ObjectResult<Type> where Type is an entity.
    >
    > How do I convert it to a List<Type>?
    >
    > I can do
    >
    > ObjectResult<Type> resultList = <<Get it from ExecuteFunction>>
    >
    > List<Type> list = new List<Type>();
    >
    > foreach (Type t in ResultList)
    >
    > list.Add(t)
    >
    > I do not want this loop. Please assist.
    >
     
    var list = new List<Type>;
     
    list = (from a in resultlist select a).Tolist();    // Linq query
     
    Wednesday, June 13, 2012 3:36 AM
  • Hi darnold924

    PrivatePyle1982, want to get a List<Entity> after executing a SQL function using ExecuteFunction.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, June 13, 2012 3:52 AM
  • Fernando,

    It complains thus:

    Error 2 'System.Data.Objects.ObjectResult<Customer>' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'System.Data.Objects.ObjectResult<Customer>' could be found (are you missing a using directive or an assembly reference?) 

    However darnold924's Linq appears to work.

    Wednesday, June 13, 2012 12:42 PM
  • Hi PrivatePyle1982;

    It is hard to say why it did not work without seeing the actual code you used with some details. You state you get this error, Error 2 'System.Data.Objects.ObjectResult<Customer>' does not contain a definition for 'ToList' ", but when you query your db through your stored procedure / Function which should be mapped through your EDMX to return a entity type of Customer, hey is that a coincidence that I used the same Entity type as you or did you forget to change it?

    When you did an import function in your EDMX of the stored procedure what was the type of entity did you say it will return?

       


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, June 13, 2012 5:30 PM
  • Hi, I stand, my name is César

    It has happened just, and my solution was:

    add in clausules using that.

    using System.Linq;

    Regards!!


    Ing. César Salazar

    Thursday, October 16, 2014 3:51 AM
  • In 2017 it doesn't work... toList doesn't exist and convert to DataContext from current context just opens multiple pipelines for no reason... it doesn't work
    Saturday, August 19, 2017 7:02 PM