使用 LINQ 从选择中排除列

新手上路,请多包涵

我正在使用实体框架代码优先开发 WCF RESTful Web 服务。

我有一张桌子 Users 有很多列。我这样做是为了获取特定用户:

 context.Configuration.ProxyCreationEnabled = false;
var users = from u in context.Users
            where u.UserId == userId
            select u;

在这个表上,有一个密码列,我不想返回这个列。

如何从该选择中排除密码列?

原文由 VansFannel 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 487
2 个回答

在 select 语句中指定您想要的每一列:

 var users = from u in context.Users

        where u.UserId == userId

        select u.UserId, u.Watever, etc... ;

原文由 Jason Enochs 发布,翻译遵循 CC BY-SA 3.0 许可协议

像这样的另一种方式,

    var users = from u in context.Users
                where u.UserId == userId
                select new
                {
                    col1 = u.UserId,
                    col2 = u.Watever
                }.ToList();

原文由 user2815037 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进