[英]entity framework column projection
I have this model: 我有这个模型:
public partial class SystemUser
{
[Display(Name = "User")]
public string Username { get; set; }
[Display(Name = "Pass")]
public string Password { get; set; }
[Display(Name = "Admin")]
public Nullable<bool> Type { get; set; }
}
this function return all record of SystemUser: 此函数返回SystemUser的所有记录:
public IEnumerable<object> GetAll()
{
var Users = Ctx.SystemUsers.ToList().Select(u => new SystemUser
{
Username = u.Username,
Type = u.Type
});
return Users.ToList();
}
but this query also have password column I dont need this.how can i remove this column from query? 但此查询也有密码列,我不需要此。如何从查询中删除此列? i dont want use anonymus type because it remove dataannotation
我不想使用匿名类型,因为它删除了数据注释
You should return Anonymous Types
in GetAll()
method. 您应该在
GetAll()
方法中返回Anonymous Types
。 Change GetAll
like this: 像这样更改
GetAll
:
public IEnumerable<object> GetAll()
{
var Users = Ctx.SystemUsers.ToList().Select(u => new
{
User = u.Username,
Admin = u.Type
});
return Users.ToList();
}
No way of doing this without raw sql. 没有原始sql,就无法做到这一点。 You always fetch entire table row with Entity Framework.
您总是使用实体框架获取整个表行。
Try this: 尝试这个:
public IEnumerable<object> GetAll()
{
return Ctx.SystemUsers.Select(u => new
{
Username = u.Username,
Type = u.Type
}).ToList();
}
You can use a DTO and AutoMapper Queryable Extensions to support this scenario:- 您可以使用DTO和AutoMapper可查询 扩展来支持这种情况:
public class SystemUserDto
{
[Display(Name = "User")]
public string Username { get; set; }
[Display(Name = "Admin")]
public Nullable<bool> Type { get; set; }
}
...
Mapper.CreateMap<SystemUser, SystemUserDto>();
...
public IEnumerable<SystemUserDto> GetAll()
{
var Users = Ctx.SystemUsers.Project().To<SystemUserDto>();
}
Based on an unnecessarily involved exchange of comments across more than one question, it sounds like what you're really asking is: 基于一个以上问题的不必要的交流意见,听起来您真正要问的是:
I have a type which includes a
Password
field, which I get from the database with Entity Framework.我有一个包含
Password
字段的类型,该字段是从Entity Framework数据库中获取的。 This type includes data annotations which I need to use in an auto-generated UI.这种类型包括我需要在自动生成的UI中使用的数据注释。 I have no control over the UI.
我无法控制UI。 How can I provide the UI with a type which includes these data annotations, but doesn't include the
Password
field?如何为UI提供包含这些数据注释但不包含
Password
字段的类型?
In that case, define another type: 在这种情况下,请定义另一种类型:
public class SystemUserViewModel
{
[Display(Name = "User")]
public string Username { get; set; }
[Display(Name = "Admin")]
public Nullable<bool> Type { get; set; }
}
And transform to that type: 并转换为该类型:
Ctx.SystemUsers.ToList().Select(u => new SystemUserViewModel
{
Username = u.Username,
Type = u.Type
});
If you use an anonymous object, you don't have a class definition so you don't have any attributes (data annotations). 如果使用匿名对象,则没有类定义,因此没有任何属性(数据注释)。 If you use your
SystemUser
type then you have a Password
property. 如果使用
SystemUser
类型,则将具有“ Password
属性。 To use something which is neither of these things, you have to define a type. 要使用这些都不是的东西,您必须定义一个类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.