简体   繁体   中英

ASP.NET Comparing data from Database

I'm currently working on a project which needs to check for the day and time, then compare these with values in my database. This needs to be done for an Live Chat, while we have opened. I'm new to ASP.Net and this is giving me an headache.

I Have a table which contains these Rows: - weekday - starttime - endtime

In my Controller for the Chat I have tried this so far

private Model1Container db = new Model1Container();

    public ActionResult Index(Openinghours openinghours)
    {
        ViewBag.Opening = db.OpeninghoursSatz.ToList();

        string CurrentDay = DateTime.Now.DayOfWeek.ToString();
        string CurrentTime = DateTime.Now.TimeOfDay.ToString();

        bool IsWeekDay= db.OpeninghoursSatz.Where(i => i.weekday == CurrentDay).Any();
        var Starttime = db.OpeninghoursSatz.Find(CurrentDay);
        Debug.Write("!!++" + Starttime);

My exact Problem is to compare the time with the two rows from my table. I thought about putting the values in a variable and then compare them, but there must be a cleaner way for this.

I hope I was able to point out what my problem is, if not please ask.

With regards,

Nico

EDIT: Maybe I should say that the database holds the hour it opens and the hour it closes (eg 9 and 17). I need to check if the current time is between these two.

      int getStartHour = DateTime.Now.Hour;
        int weekDay= (int)DateTime.Now.DayOfWeek;
 bool value =    db.OpeninghoursSatz.Where(m => getStartHour >= m.Starttime  && getStartHour <= m.EndTime && m.weekday == weekDay).Any()

    /// if everything is in int. or if in string 
     string getStartHour = DateTime.Now.Hour.ToString();
        string weekDay= DateTime.Now.DayOfWeek.ToString();
 bool value =    db.OpeninghoursSatz.Where(m => getStartHour >= m.Starttime  && getStartHour <= m.EndTime && m.weekday == weekDay).Any();

But in this case for Thursday , we will get Thursday, but in Int we will get 4.

The line looks odd to me:

var Starttime = db.OpeninghoursSatz.Find(CurrentDay);

I guess you should change it to something like:

if (IsWeekDay){
    var Starttime = db.OpeninghoursSatz.Where(i => i.weekday == CurrentDay).First().StartTime;
    Debug.Write("!!++" + Starttime);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM