开发者

Asp.net MVC 3 Linq DateTime

开发者 https://www.devze.com 2023-03-20 18:13 出处:网络
I need a linq statement that returns all entries from a certain date. A the moment I have a class in my controller which handles Events. My Index class contains a linq statement which groups the vents

I need a linq statement that returns all entries from a certain date. A the moment I have a class in my controller which handles Events. My Index class contains a linq statement which groups the vents by date and returns how many there are in each date. I want a browse class which returns a list of Events connected with a certain date. Here is my mode:

  namespace NewAtAClick.Models
  {
    public class WhatsOn
    {

    public int ID { get; set; }
    public DateTime? start { get; set; }
    public DateTime? end { get; set; }
    public string Name { get; set; }
    public string Desc { get; set; }
    public string link { get; set; }
    public bool CalenderDisplay { get; set; }
    public DateTime? day { get; set; }
    public int whtscount { get; set; }
    }
  }

And here's my classes in the WhatsOn controller;

   public ViewResult Index(WhatsOn model)
   {
        DateTime myDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);

        var datequery =
                        db.WhatsOns.Where(c => c.start > myDate)
                        .OrderByDescending(c => c.start)
                        .GroupBy(c => c.start).AsEnumerable().Select(
                        sGroup => new WhatsOn
                        {
                            day = sGroup.Key,
                            whtscount = sGroup.Count()
                        });

        return View(datequery);
    }

    public ViewResult Browse(DateTime? day , int? id)
    {         
        var eventsquery = from c in db.WhatsOns
                          where c.start == day
   开发者_如何学Go                        select c;

        return View(eventsquery);
    }

A the moment the linq query in the browse class returns nothing, just an empty table. Any help is greatly appreciated! Thanks.

UPDATE:

Hey! Got it working

Here;s my new controller;

    public ViewResult Browse(int? id, DateTime? day, DateTime? start)
    {

        var eventsquery = from c in db.WhatsOns where c.start.Value.Day == day.Value.Day select c;

        return View(eventsquery);


    }

And what did the trick, in my actionlink in my view....

 @Html.ActionLink("Browse", "Browse", new { start=item.start, day=item.day })

Thanks for you help!!


does

var eventsquery = from c in db.WhatsOns
                  where c.start.Value.Date == day.Value.Date
                  select c;

work?


When comparing DateTime objects, keep in mind that the '==' sign also looks at seconds, miliseconds, etc.

Do you have any results you DO expect? Is the database table filled with information?

Edit: DateTime.Now you already used ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消