开发者

EF code first return subset of columns

开发者 https://www.devze.com 2023-03-13 17:28 出处:网络
I have a table with about 12 columns.But there are time when I only need 2 or 3 columns. I am using Entity Framework code first to query the database from my website.

I have a table with about 12 columns. But there are time when I only need 2 or 3 columns.

I am using Entity Framework code first to query the database from my website.

What should I do so that I do not query all the columns but only a subset开发者_运维知识库 of columns?

Thanks, Mike


You can use a projection to an anonymous type that contains the properties you want, i.e if your Foo entity has the properties A, B and C you could do:

var results = context.Foos.Select( x => new { x.A, x.B });

to retrieve only the properties A and B.

0

精彩评论

暂无评论...
验证码 换一张
取 消