Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我可以只从一个实体加载几个属性吗?
例如,我有一个具有以下属性的实体:
ID DESCRIPTION HEADER PICTURE
我只想加载 ID 而不是其他属性。
我怎样才能做到这一点?
在您的情况下,如果您只需要IDs,则可以使用以下查询:
ID
var ids = context.YourEntities.Select(e => e.ID).ToList();
您还可以使用投影(如果您需要加载多个属性,则很有用):
var entitiesWithIdsAndHeaders = context. YourEntities. Select(e => new { Id = e.ID, Description = e.Description }). ToList();