scarpacci
scarpacci

Reputation: 9194

How can I create a List off of an Entity Framework entity (object)?

Can I use the Entity Framework Model to allow me to create a list off of an existing table (entity)? If so I just can't figure out how to do that?

So for instance if I have a table in my DB called Order and that has been added to Entity Framework can I use the Entity Framework model to generate a List of Objects?

using SQL Server 2008 R2 and Visual Studio 2010

var orderContext = new OrderEntities();

var order = orderContext.Order;

List<order> ordersList = new List<order>();

That would be cool if I could.

Thanks,

S

Upvotes: 1

Views: 6052

Answers (2)

Slovo
Slovo

Reputation: 222

"order" its a variable, you must create List for Type as:

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

And then you can:

list = _context.Order.ToList();

(need using System.Linq)

Upvotes: 0

Rohan West
Rohan West

Reputation: 9298

Make sure that you are using System.Linq, you should be able to do the following. Presuming that Order is a ObjectSet.

using System.Linq;

Then use the ToList extension method.

IList<Order> orders = orderContext.Order.ToList();

Upvotes: 3

Related Questions