[RESOLVED]Using enum to represent days of the week

Hello,

I’m trying to use Enum for day of week and ger results according for current day .

I did something like this and I getting this error message :

    public enum MyDayOfWeek
    {
        Sunday = 0,
        Monday = 1,
        Tuesday = 2,
        Wednesday = 3,
        Thursday = 4,
        Friday = 5,
        Saturday = 6,
    }
    public class Course
    {
        public int CourseId { get; set; }
        public string CourseName { get; set; }
        public int Hour { get; set; }
        public MyDayOfWeek Days { get; set; }
    }

   public ActionResult coursePge()
        {
            int MyDayOfWeek = 0;

            DateTime dt = DateTime.Today;
            if (dt.DayOfWeek == DayOfWeek.Sunday) MyDayOfWeek = 0;
            else if (dt.DayOfWeek == DayOfWeek.Monday) MyDayOfWeek = 2;
            else if (dt.DayOfWeek == DayOfWeek.Tuesday) MyDayOfWeek = 3;
            else if (dt.DayOfWeek == DayOfWeek.Wednesday) MyDayOfWeek = 4;
            else if (dt.DayOfWeek == DayOfWeek.Thursday) MyDayOfWeek = 5;
            else if (dt.DayOfWeek == DayOfWeek.Friday) MyDayOfWeek = 6;
            else if (dt.DayOfWeek == DayOfWeek.Saturday) MyDayOfWeek = 7;

            var TodaysCource = (from c in db.Courses.Where(d=>d.Days.Equals(MyDayOfWeek))
                                select c).OrderBy(d => d.Days)
                              .ThenBy(t => t.Hour);
            return View(TodaysCource.ToList());
            //var CheckMySelf = (from vv in db.Courses.Where(dd => dd.Days.Equals(DateTime.Today))
            //                   select vv);
            //return View(TodaysCource);
            
        }

@model IEnumerable<QDeleteMe.Models.Course>

@{
    ViewBag.Title = "coursePge";
}

<h2>coursePge</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
   

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.CourseName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Hour)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Days)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.CourseId }) |
            @Html.ActionLink("Details", "Details", new { id=item.CourseId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.CourseId })
        </td>
    </tr>
}

</table>

Unable to create a constant value of type ‘System.Object’. Only primitive types or enumeration types are supported in this context.             

Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.            

Exception Details: System.NotSupportedException: Unable to create a constant value of type ‘System.Object’. Only primitive types or enumeration types are supported in this context.
Source Error:

Line 38:                                 select c).OrderBy(d => d.Days)
Line 39:                               .ThenBy(t => t.Hour);
Line 40:             return View(TodaysCource.ToList());

Solv.

Hi assafg2,

Base on the error message, it means the enum condition is not supported.

You need to use the normal type (e.g. int) in the where condition.

Best Regards

Starain

hello,

changing to this did the trick

DateTime dt = DateTime.Today;
            MyDayOfWeek = (int)dt.DayOfWeek;

var TodaysCource = (from c in l.Where(d=>(int)d.Days==(MyDayOfWeek))
                                select c

Leave a Reply