load partial entityset ef4

自古美人都是妖i 提交于 2019-12-08 06:51:02

问题


Can I load only a few properties from an Entity?

As an example I have an entity with the following properties:

ID
DESCRIPTION
HEADER
PICTURE

I only want to load the IDs and not the other properties.

How can I do this?


回答1:


In your case if you just need the IDs, you can use the following query:

var ids = context.YourEntities.Select(e => e.ID).ToList();

You can also use projection (useful if you need to load more than one property):

var entitiesWithIdsAndHeaders = context.
                                YourEntities.
                                Select(e => new
                                            {
                                                Id = e.ID,
                                                Description = e.Description
                                            }).
                                ToList();


来源:https://stackoverflow.com/questions/5311678/load-partial-entityset-ef4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!