C# 使用 LINQ 从选择中排除一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19463099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exclude a column from a select using LINQ
提问by VansFannel
I'm developing a WCF RESTful web service with Entity Framework Code First.
我正在使用 Entity Framework Code First 开发 WCF RESTful Web 服务。
I have a table Users
with a lot of columns. I do this to get an specific user:
我有一张Users
有很多列的表。我这样做是为了获得特定用户:
context.Configuration.ProxyCreationEnabled = false;
var users = from u in context.Users
where u.UserId == userId
select u;
On this table, there is a password column, and I don't want return this column.
在这个表上,有一个密码列,我不想返回这一列。
How can I exclude password column from that select?
如何从该选择中排除密码列?
采纳答案by Jason Enochs
Specify each column that you do want in your select statement:
在 select 语句中指定您需要的每一列:
var users = from u in context.Users
where u.UserId == userId
select u.UserId, u.Watever, etc... ;
回答by Digvijay Verma
Its sad to say but NO
很遗憾地说,但不
You do not have option to directly exclude any particular column. You may go with lazy loading of columns.
您无法直接排除任何特定列。您可以使用延迟加载列。
The easiest and non-liking method would be to include columns which you want.
最简单且不受欢迎的方法是包含您想要的列。
回答by Digvijay Verma
another way like this,
像这样的另一种方式,
var users = from u in context.Users
where u.UserId == userId
select new
{
col1 = u.UserId,
col2 = u.Watever
}.ToList();
回答by Brent Lamborn
You can create more than one LINQ object per table. I'd create one with the field you need and one without. It makes CRUD operations more difficult though.
您可以为每个表创建多个 LINQ 对象。我会用你需要的领域创建一个,一个没有。但是,它使 CRUD 操作更加困难。