Retrieving contional value inside Inlude in entity framework

.net-core angular entity-framework entity-framework-6 entity-framework-core

Question

I have a dotnet core 2.0 backend and angular2+ frontend application.

I would like to get a list of participants where event field is equal to "baseline" (comes from dropdown menu in the HTML) in the schedules class. I have a one-to-many relationship between Participant and Schedule class.

Participant.cs class:

public class Participant
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Gender { get; set; }
    public DateTime DateOfBirth { get; set; }

    // RelationShips
    public ICollection<Schedule> Schedules { get; set; }

    public Participant()
    {
        Schedules = new Collection<Schedule>();
    }
}

And my Schedule.cs class:

public class Schedule
{
    public int Id { get; set; }
    public DateTime? AppointmentDate { get; set; }
    public string AppointmentTime { get; set; }
    public string Event { get; set; }
    public string Location { get; set; }

    // RelationShip
    public Participant Participant { get; set; }
    public int ParticipantId { get; set; }
}

I have an angular 2+ frontend to use this to get the list of participants and their schedules where the event is equal to "baseline". The baseline is coming from the frontend. Basically, a hard-coded drop-down menu item.

I am able to get all schedules with all events as below:

return await context.Participants
            .Include(x => x.Schedules)
            .ToListAsync();

But this gives me again people with all schedules. I need people with all schedule where event is equal to "baseline".

Here is what I came up with so far:

return await context. Participants
            .Include(x => x.Schedules.Where(v => v.Event == "baseline"))
            .ToListAsync();

But this gives me nothing. What would be the way to accomplish this? I don't remember where I read but someone said entity framework doesn't support this yet. I am not sure this is true.. Any help would be appreciated.

EDIT: Here is error stack I have:

InvalidOperationException: The Include property lambda expression 'x => {from Schedule v in x.Schedules where [v].Event.ToLower().Equals(__ToLower_0) select [v]}' is invalid. The expression should represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, specify an explicitly typed lambda parameter of the target type, E.g. '(Derived d) => d.MyProperty'. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=74639

1
0
6/28/2018 3:11:04 PM

Accepted Answer

If you need to get a list of participants you could go the other way around, starting at schedules and getting the Participant references like this:

return context.Schedules.Where(s => s.Event == "baseline")
.Include(x => x.Participant).Select(x => x.Participant)
0
7/2/2018 9:06:44 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow