Enum.ToString不起作用

时间:2013-02-05 08:48:46

标签: c# linq-to-sql enums

检查此代码小部件。

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()总是返回字节值。

1 个答案:

答案 0 :(得分:4)

我怀疑问题是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()
                    });