使用LINQ从选择中排除列

我正在使用Entity Framework Code First开发WCF RESTful Web服务。

我有一个表有很多列的Users 。 我这样做是为了得到一个特定的用户:

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

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

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

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

 var users = from u in context.Users where u.UserId == userId select u.UserId, u.Watever, etc... ; 

可悲的是,但不是

您无法直接排除任何特定列。 您可以使用延迟加载列。

最简单且不喜欢的方法是包含您想要的列。

另一种方式,

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

您可以为每个表创建多个LINQ对象。 我会创建一个你需要的领域,一个没有。 但它使CRUD操作更加困难。