将不可读的数据库数据显示为可读格式
Displaying non readable DB data into readable format
我有一个 table,其中包含一个只有数字的字段。我想要实现的是将枚举中的实际数字和 "translate" 数字表示为可读的值。
例如,我有一个用户 table,其中包含一个名为用户角色的字段。有4种角色:super admin
、admin
、manager
、regular user
.
Super Admin is represented as 0
Admin is represented as 1
Manager is represented as 2
ManagerAdmin is represented by 3 (combination of admin = 1 and Manager = 2)
Regular user is represented as 4
尝试从 Enum = 3 翻译时如何显示 Manager; Admin
?
UserModel
public int UserRoles {get; set;}
public string UserNames {get; set;}
public string UserAddress {get; set;}
Enum
public enum UserRole
{
SuperAdmin = 0,
Admin = 1,
Manager = 2,
Regular = 3
ManagerAdmin = 4
}
C# Code
public IEnumerable<User> UserInfo()
{
var userInfo = context.User.Select(u => new UserModel
{
UserRoles = u.Roles, //this is where I want the actual string roles
UserNames = u.Names,
UserAddress = u.Address
}).ToList();
}
//I was thinking something like this:
if(u.Roles == 0)
{
// project Super Admin
}
else if(u.Roles == 1)
{
// project Admin
} etc...
Enum.GetName(typeof(UserRole), enumValue)
会为您提供您正在寻找的 Rolename
。这里 enumValue
将是 0,1,2,3
var userInfo = context.User.Select(u => new UserModel
{
UserRoles =Enum.GetName(typeof(UserRole), u.Roles) ,
UserNames = u.Names,
UserAddress = u.Address
}).ToList();
我有一个 table,其中包含一个只有数字的字段。我想要实现的是将枚举中的实际数字和 "translate" 数字表示为可读的值。
例如,我有一个用户 table,其中包含一个名为用户角色的字段。有4种角色:super admin
、admin
、manager
、regular user
.
Super Admin is represented as 0
Admin is represented as 1
Manager is represented as 2
ManagerAdmin is represented by 3 (combination of admin = 1 and Manager = 2)
Regular user is represented as 4
尝试从 Enum = 3 翻译时如何显示 Manager; Admin
?
UserModel
public int UserRoles {get; set;}
public string UserNames {get; set;}
public string UserAddress {get; set;}
Enum
public enum UserRole
{
SuperAdmin = 0,
Admin = 1,
Manager = 2,
Regular = 3
ManagerAdmin = 4
}
C# Code
public IEnumerable<User> UserInfo()
{
var userInfo = context.User.Select(u => new UserModel
{
UserRoles = u.Roles, //this is where I want the actual string roles
UserNames = u.Names,
UserAddress = u.Address
}).ToList();
}
//I was thinking something like this:
if(u.Roles == 0)
{
// project Super Admin
}
else if(u.Roles == 1)
{
// project Admin
} etc...
Enum.GetName(typeof(UserRole), enumValue)
会为您提供您正在寻找的 Rolename
。这里 enumValue
将是 0,1,2,3
var userInfo = context.User.Select(u => new UserModel
{
UserRoles =Enum.GetName(typeof(UserRole), u.Roles) ,
UserNames = u.Names,
UserAddress = u.Address
}).ToList();