枚举返回 int 而不是字符串 C#
Enum returning int instead of string C#
枚举 Class 我打算在这里提取男性和女性的价值
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime DoB { get; set; }
public enum MF { Male,Female }
public MF Gender { get; set; } //here's my target
}
数据访问层是我从我的数据库 msql 中提取的地方
public IList<Employee> GetAllEmployees()
{
string query = "EXEC GetAllEmployees";
string constring = "Data Source=.\SQLEXPRESS;Initial Catalog=ProjectDatabase;Integrated Security=True;Pooling=False";
IList<Employee> AllEmployees = new List<Employee> {};
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
List<Employee> customers = new List<Employee>();
//cmd.CommandType = CommandType.Text;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
AllEmployees.Add(new Employee
{
ID = Convert.ToInt32(sdr["ID"]),
Name = sdr["Name"].ToString(),
DoB = (DateTime)sdr["DoB"],
Gender = (MF)Enum.Parse(typeof(MF), (string)sdr["Gender"]), //here's where it extracts the Gender value as 0 or 1 instead in plain words
});
}
}
con.Close();
return AllEmployees;
}
}
}
业务逻辑层自我解释
public IList<Employee> GetEmployees(string name,MF gender)
{
EmployeeDAL EDAL = new EmployeeDAL();
if (name == "")
return EDAL.GetAllEmployees(); //Ignore this for now
else
return EDAL.GetFilteredEmployees(name,gender); //this gets used in this case
}
一切开始的控制器层
[Route("GetEmployees")]
[HttpPost]
public IList<Employee> GetEmployees(JArray PostData)
{
string Name = (string)PostData[0]["Name"];
MF Gender = (MF)Enum.Parse(typeof(MF), (string)PostData[0]["Gender"]);//grabed from a post request from AngularJS code
EmployeeBLL EBLL = new EmployeeBLL();
return EBLL.GetEmployees(Name,Gender);
}
大家好,对于 AngularJS POST 请求,我想将我的性别枚举用于 return 男性或女性,但我一直得到 0 表示男性,1 表示女性.我该如何解决?评论中的所有其他详细信息。
对您来说,由于命名方式,枚举看起来像字符串,但实际上它们是数字,至少在计算机看来是这样。您可以调用 .ToString()
获取名称作为一个 stirng。
枚举 Class 我打算在这里提取男性和女性的价值
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime DoB { get; set; }
public enum MF { Male,Female }
public MF Gender { get; set; } //here's my target
}
数据访问层是我从我的数据库 msql 中提取的地方
public IList<Employee> GetAllEmployees()
{
string query = "EXEC GetAllEmployees";
string constring = "Data Source=.\SQLEXPRESS;Initial Catalog=ProjectDatabase;Integrated Security=True;Pooling=False";
IList<Employee> AllEmployees = new List<Employee> {};
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
List<Employee> customers = new List<Employee>();
//cmd.CommandType = CommandType.Text;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
AllEmployees.Add(new Employee
{
ID = Convert.ToInt32(sdr["ID"]),
Name = sdr["Name"].ToString(),
DoB = (DateTime)sdr["DoB"],
Gender = (MF)Enum.Parse(typeof(MF), (string)sdr["Gender"]), //here's where it extracts the Gender value as 0 or 1 instead in plain words
});
}
}
con.Close();
return AllEmployees;
}
}
}
业务逻辑层自我解释
public IList<Employee> GetEmployees(string name,MF gender)
{
EmployeeDAL EDAL = new EmployeeDAL();
if (name == "")
return EDAL.GetAllEmployees(); //Ignore this for now
else
return EDAL.GetFilteredEmployees(name,gender); //this gets used in this case
}
一切开始的控制器层
[Route("GetEmployees")]
[HttpPost]
public IList<Employee> GetEmployees(JArray PostData)
{
string Name = (string)PostData[0]["Name"];
MF Gender = (MF)Enum.Parse(typeof(MF), (string)PostData[0]["Gender"]);//grabed from a post request from AngularJS code
EmployeeBLL EBLL = new EmployeeBLL();
return EBLL.GetEmployees(Name,Gender);
}
大家好,对于 AngularJS POST 请求,我想将我的性别枚举用于 return 男性或女性,但我一直得到 0 表示男性,1 表示女性.我该如何解决?评论中的所有其他详细信息。
对您来说,由于命名方式,枚举看起来像字符串,但实际上它们是数字,至少在计算机看来是这样。您可以调用 .ToString()
获取名称作为一个 stirng。