Sean
Sean

Reputation: 907

Return specific element in entity framework

I'm wanting the ability to create a method where I can check against any entity held in my table via one call. Right now, I only know how to return the first element in my table like so:

 EntityModel.Entity entity = (from e in context.Entities
                                     select e).FirstOrDefault();

However, I'm wanting to grab the third entity held in my context. How can I achieve this?

Upvotes: 0

Views: 63

Answers (1)

Habib
Habib

Reputation: 223312

However, I'm wanting to grab the third entity held in my context.

You can use Skip but before that you should Order your collection like:

var entity = context.Entities
                    .OrderBy(r=> r.SomeField)
                    .Skip(2)
                    .FirstOrDefault();

There is no concept of order in table's data, unless some order is explicitly specified. So if you use Skip without OrderBy, you will not be guaranteed to get same item every time with your query.

Upvotes: 4

Related Questions