4

I have a stored procedure that returns a dynamic query, e,g if i pass some value to its id parameter it return me a dynamic query like

Select * from someTable tbl where tbl.Id=51

then i execute this query using ExecuteStoreQuery like

string query = container.CreateQuery<string>(
                    "SELECT VALUE DB.Store.GetQuery(@ID) FROM {1}",
                    new System.Data.Objects.ObjectParameter("ID", 51)
                ).First();

object lists = container.ExecuteStoreQuery<object>(query);

the problem is container.ExecuteStoreQuery<object>(query); returns multiple rows which i want to get into a list, how can i do that

3 Answers 3

7

create a model of type you want to return the results like

public class mymodel{

public int _key{get;set;}
public string _value{get;set;}
}

where _key and _value correspond to the columns of the returned result

execute the query ExecuteStoreQuery also return the result AsQueryable

 container.ExecuteStoreQuery<mymodel>(query).AsQueryable().ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer. I was had a class with just members variables (eg: public int key;) and it didn't work, but when I changed the members to properties by adding the {get; set;} it worked great.
4

I'm not sure if I understand your question but it looks like you are looking for ToList method:

List<MyEntity> list = container.ExecuteStoreQuery<MyEntity>(query).ToList();

The bigger problems is object in your code - if you really mean object type it will not work. You must provide a real type (either mapped entity, complex type or custom class with public properties using same names as columns in result set) otherwise EF will not fill data for you.

1 Comment

Where did you learn this from?
1

Stored Procedures can be imported in your Entity Model. That will result in a function on your ObjectContext that you can call from your code.

When you map the SP you can let EF map the result of your SP to an Entity. If you don't have a matching entity EF can determine the resulting columns for your stored procedure and map that a complex type.

Here is the MSDN documentation that explains this.

1 Comment

tnx for replying i know the SP can be mapped to the entity but i am only using the SP to generate the query then i am executing the query by using context.ExecuteStoreQuery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.