Teddy
Teddy

Reputation: 304

Enum.ToString Not Working

Check This Code Sniplet out.

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

...

AttendanceDirection is enum Which is...

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

The Problem is The Direction = ((AttendanceDirection)at.direction.Value).ToString() is always returning the byte value.

Upvotes: 0

Views: 684

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500815

I suspect the problem is that the ToString is being effectively performed at the database side, which doesn't know the enum names. Try this:

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()
                    });

Upvotes: 4

Related Questions