Graknol
Graknol

Reputation: 55

MVC 4 SQL Server, how can i get the first record in a table?

How can I get the first record in a table independent of the rows primary key?

I'm using MVC 4, with EF Code First and a database context, or will I have to use LINQ for this?

I think I can use db.ToList() to get the list and then loop through the specific elements, but wouldn't that "download" the whole table?

Cause if it does Isn't it bad practice and inefficient?

EDIT: Meant to say "first record", sorry for any confusion :)

Upvotes: 0

Views: 2555

Answers (1)

testCoder
testCoder

Reputation: 7385

Have you tried:

db.List.First();
  • If you don't have order by clause the rows will be ordered according to clustered index (primary key by default)
  • If you have order by clause rows will be ordered by your specified order

Upvotes: 2

Related Questions