0

I'm getting pretty desperate here since this code was working before I had some unfortunate git issues and had to wipe out my project and checkout again. For some reason my IQueryable objects are not allowing me to reference properties anymore. In my code below, evt.id should be a valid but id is not recognized as a property of evt in Visual Studio. Also, I should mention that I can't access not only evt but also can't access any properties that belong to goal. Anything I missed when recoding this portion of my controller?

    public HttpResponseMessage GetTodaysPatientGoals(int id)
    {
        var evt = db.patient_event.Where(e => (e.patient_id == id) && (e.date == DateTime.Today));

        //evt.id is not recognized here either
        if(evt != null)
        {
            //evt.id is not recognized as a property of evt here
            //also cant access properties of goal either 
            var goal = db.patient_goals.Where(g => (g.patient_id == id) && (g.event_id == evt.id)).Select(row => (new { row.id, row.completed, row.goal }));
            return Request.CreateResponse(HttpStatusCode.OK, goal);
        }

        return Request.CreateResponse(HttpStatusCode.NoContent);
    }

Any help would be greatly appreciated.

user3704351
  • 67
  • 2
  • 6
  • What is meant by `not recognized`? Are you getting exception/console log? – rt2800 Apr 01 '16 at 06:07
  • Yes, I get an exception Severity Code Description Project File Line Suppression State Error CS1061 'IQueryable' does not contain a definition for 'id' and no extension method 'id' accepting a first argument of type 'IQueryable' could be found (are you missing a using directive or an assembly reference?) SpectrumWebApp C:\Projects\SpectrumWebApp\SpectrumWebApp\SpectrumWebApp\Controllers\patient_goalsController.cs 154 Active – user3704351 Apr 01 '16 at 06:07
  • Can you post the exception/stacktrace to your question? – Pedro G. Dias Apr 01 '16 at 06:08
  • Wierd thing was that this code worked great before I had to delete my whole project and clone again from my repo, not sure what happened. – user3704351 Apr 01 '16 at 06:09
  • 1
    Are you getting multiple records? Then the `evt` is collection , not a single object – rt2800 Apr 01 '16 at 06:10
  • Ah, let me try and add FirstOrDefault, I think that's the line I forgot to copy over – user3704351 Apr 01 '16 at 06:11
  • evt.id is not recognized as evt is an IQueryable. If using 'Where' you have to loop through the returned patient_event. Use FirstOrDefault to return only one object to access the property of patient_event – Hasta Tamang Apr 01 '16 at 06:15
  • Issue solved, thanks a million mate, how do I give your credit for the solution? – user3704351 Apr 01 '16 at 06:18

2 Answers2

0

When you do

var evt = db.patient_event.Where(e => (e.patient_id == id) && (e.date == DateTime.Today));

your evt will be an IEnumerable<T>, where T is whatever is in db.patient_event. That is, your evt is a collection of events and that collection doesn't have an id property. You will have to choose a particular event before you can get to the id property. For example

var myEvent = evt.First();
Console.WriteLine(myEvent.id);
Rune
  • 8,340
  • 3
  • 34
  • 47
0

get evt obj this way,

var evt = db.patient_event.Where(e => (e.patient_id == id) && (e.date == DateTime.Today)).FirstOrDefault();
levent
  • 3,464
  • 1
  • 12
  • 22