Event property not being recognized in my Web API Controller

83 Views Asked by At

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.

2

There are 2 best solutions below

0
levent On BEST ANSWER

get evt obj this way,

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

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