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

2
  • not related to the issue specifically but your mydate declaration would be simpler as DateTime myDate = DateTime.Now.Date; Commented Jul 14, 2011 at 10:52
  • Thanks, I'm aware of that, I'' change it later. Thanks though. Commented Jul 14, 2011 at 10:58

2 Answers 2

1

does

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

work?

Sign up to request clarification or add additional context in comments.

12 Comments

Be wary though, check for nulls before the comparisons because your datetimes are nullable
@Dan, sorry my fault you need to add the .Value first because its a nullable date time
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 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
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...
|
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 ;)

5 Comments

Also, I presume db.WhatsOns is a typo (the extra 's')
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!
db.WahtsOns is not a typo. This is the 'DBSet' name in my NewAtAClick.Models datacontext. It represents a table in code first
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.
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.