Enum.ToString Not Working

本文关键字:Working Not ToString Enum | 更新日期: 2023-09-27 17:57:34

检查此代码段。

using (AttendanceDataContext db = new AttendanceDataContext())
{
var attendance = db.attendpunches.Select(at => new RawEmployeeCheckInOutInfo
                    {
                        CheckTime = at.punchtime.Value,
                        Direction = ((AttendanceDirection)at.direction.Value).ToString()
                    });

AttendanceDirection是enum哪个是…

public enum AttendanceDirection : int
{
    CheckIn = 1,
    CheckOut = 2
}

问题是Direction=((AttendanceDirection)at.Direction.Value).ToString()总是返回字节值。

Enum.ToString Not Working

我怀疑问题在于ToString在数据库端有效执行,而数据库端不知道枚举名称。试试这个:

var attendance = db.attendpunches
                   .Select(at => new { CheckTime = at.punchtime.Value, 
                                       Direction = at.direction.Value })
                   .AsEnumerable() // Do the rest of the query in-process...
                   .Select(at => new RawEmployeeCheckInOutInfo {
                       CheckTime = at.CheckTime,
                       Direction = ((AttendanceDirection) at.Direction).ToString()
                    });