简体   繁体   English

MVC 4 SQL Server,我怎样才能获得表中的第一条记录?

[英]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? 我正在使用MVC 4,EF Code First和数据库上下文,还是我必须使用LINQ?

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? 我想我可以使用db.ToList()来获取列表然后遍历特定元素,但是不会“下载”整个表吗?

Cause if it does Isn't it bad practice and inefficient? 如果它确实是不是不好的做法和效率低下?

EDIT: Meant to say "first record", sorry for any confusion :) 编辑:意味着说“第一条记录”,对不起任何混乱:)

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) 如果您没有order by子句,则将根据聚簇索引(默认情况下为主键)对行进行排序
  • If you have order by clause rows will be ordered by your specified order 如果您有order by子句,则行将按您指定的顺序排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 NodeJS 中的 SQL 服务器获取列表记录 - How can I get list record from SQL Server in NodeJS 如何在SQL Server中仅选择每个外键的第一条记录? - How can I select only the first record with each foreign key in SQL Server? 如何从 SQL 服务器中的同一组中获取第一条和最后一条记录? - How to get first and last record from same group in SQL Server? 如何从SQL Server列表中获取第一条记录 - How to get the first record from a list in SQL Server SQL Server 2008 如何获取每组的第一条和最后一条记录? - How to get the first and the last record per group in SQL Server 2008? 如何将从ms访问表检索的记录插入到SQL Server表中? - How can I insert a record retrieved from an ms access table into an sql server table? 在 Sql 服务器中,如何从表 B 的多个记录中更新表 A 记录值 - In Sql Server, how can I update Table A record values from multiple records of Table B SQL Server 临时表:我可以记录谁改变了什么? - SQL Server Temporal Table: can I record who changed what? SQL Server-从组中获取第一条记录 - Sql Server - Get first record from a group 如何从SQL Server表中获取JSON对象? - How can I get a JSON object from a SQL Server table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM