0

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
                           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!!

Dan
  • 1,450
  • 1
  • 17
  • 34

2 Answers2

1

does

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

work?

Manatherin
  • 4,169
  • 5
  • 36
  • 52
  • Be wary though, check for nulls before the comparisons because your datetimes are nullable – Manatherin Jul 14 '11 at 10:25
  • @Dan, sorry my fault you need to add the .Value first because its a nullable date time – Manatherin Jul 14 '11 at 10:38
  • Right, I popped that in and it still returns nothing. But you're right. I only need to compare days, so I'll leave it in. Thanks for your help. Do you think it's my model? Shoul I seperate the concepts of 'Event' (WhatsOn) and 'Date'? – Dan Jul 14 '11 at 10:44
  • @Dan im not really sure why that wouldn't work, unless there arnt any entries in whats on with a start date that matches the day date – Manatherin Jul 14 '11 at 10:53
  • My fault, I had .day instead of .date. I am getting another error now, but you've definitely gotten me close. Here is the error...The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported... – Dan Jul 14 '11 at 10:57
  • @Dan I'd never seen that before although it does seem to be a known issue, there is a stackoverflow question on it http://stackoverflow.com/questions/1478215/how-to-compare-only-date-components-from-datetime-in-c – Manatherin Jul 14 '11 at 11:17
  • Yeah, I just found that. I'll have a read and see what I can do. Thanks for all your help Manatherin! – Dan Jul 14 '11 at 11:27
  • @Dan i'd be really careful with your current solution as you are using the Day variable there (i.e. today is the 14th so the Day variable = 14) so the matches if the system is in use for more than a month are not going to be correct – Manatherin Jul 14 '11 at 14:46
  • Thanks Mantherin, I'll change it to .Date and see what happens.Appreciate all your help! – Dan Jul 14 '11 at 14:50
  • linq can't handle .Date or .DayOfYear for some reason. I'll keep trying. This is the rror...The specified type member 'DayOfYear' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.... You have been a great help already. Thanks! – Dan Jul 14 '11 at 14:58
  • I does seem pretty strange, at the moment all i can think of is to either do a `((Day == Day) && (Month == Month) && (Year == Year))`, or a string comparison by calling `ToShortDateString()`, neither of those suggestions seem particularly good or efficient though – Manatherin Jul 14 '11 at 15:07
  • Better than what I had in mind! There's a great developer I know that has just built quite a large app using this stuff. From what I can remember he had to have gotten around this somehow. I'll email him and report back here tomorrow. – Dan Jul 14 '11 at 15:17
0

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

Peanutbag
  • 277
  • 1
  • 4
  • 11
  • Also, I presume db.WhatsOns is a typo (the extra 's') – Peanutbag Jul 14 '11 at 10:31
  • I have resultsd in the table. I can update and enter new results and my results counter whtscount is working and returning that there is two results. Thanks for the tip, I will try and make it look at the day. Any more advice would be great. Thanks! – Dan Jul 14 '11 at 10:34
  • db.WahtsOns is not a typo. This is the 'DBSet' name in my NewAtAClick.Models datacontext. It represents a table in code first – Dan Jul 14 '11 at 10:34
  • Ah yea, forgot about that automatic pluralize thing! Hehe okay! You might also want to take a look at DateTime.Compare(param1, param2) or just the param1.Equals(param2) method. – Peanutbag Jul 14 '11 at 10:41
  • Thanks Peanutbag, that's a good point. I'll have a reads about that and try it. I'm quite new to all this so your help is appreciated! – Dan Jul 14 '11 at 10:42