简体   繁体   中英

Entity Framework One to Many Relationship Model - How to select column from the child table

My overall goal is to return awards with TeamIds that match a list of IDs passed in from a filter. I'm looking for

  1. Validation that my model is built correctly
  2. Help with writing the code to return the awards (needs to have a return type of IQueryable).

Here is a simplified version of my model. One Award can have many teams associated with it. Additionally one team can have many awards (but I don't really care about that relationship. I only need to go from Award to Team).

public class Award
    {
        [Key]
        [ForeignKey("Teams")]
        public int AwardId { get; set; }

        //navigation property
        public virtual ICollection<AwardTeamMap> Teams{ get; set; }

    }


    public class AwardRoleMap
    {

        [Key, Column(Order = 0)]
        public int AwardId { get; set; }

        [Key, Column(Order = 1)]
        public int TeamId { get; set; }


    }

In my dbcontext, I have this:

modelBuilder.Entity<Award>()
                .HasMany(m => m.GuruTeams)
                .WithOptional()
                .HasForeignKey(a => a.AwardId);

Now, I am trying to return the awards that have TeamIds that match ints provided by a filter that the user interacts with (filter.IntValues).

When I run this, I can this run time error: Only primitive types or enumeration types are supported in this context.

I feel there should be a better way to get TeamIds, but I am struggling.

var awards = from award in baseAwardQuery
                         from AwardTeams in db.Awards.Queryable.Where(a => filter.IntValues == a.Teams.Select(x => x.TeamId)
                          && a.AwardId == award.AwardId)
                          select award;

I don't understand your meaning a bit I guess this is what you mean. Input is: Collection of team id Output expected: All award of each team id above have got

First, you got these Entity Model

public class Team
{
    [Key]
    public int Id { get; set; }
    public virtual ICollection<TeamAward> AwardOfTeam { get; set; }

}

public class TeamAward
{
    public int TeamId { get; set; }
    public int AwardId { get; set; }

    public virtual Team Team { get; set; }
    public virtual Award Award { get; set; }
}

public class Award
{
    [Key]
    public int Id { get; set; }
    public virtual ICollection<TeamAward> TeamHasAward { get; set; }

}

And then, in your function you can query like this

// I don't know what is your filter.IntValues so I guess it is array of int
int[] teamIds = new[] { 1, 3, 4 };
// This is list of all award that Team 1, Team 3 and Team 4 got
List<Award> awards = context.Teams
                     .Where(team => teamIds.Contains(team.Id))
                     .SelectMany(team => 
                                 team.AwardOfTeam.Select(awardOfTeam =>
                                                         awardOfTeam.Award)).ToList();
// Do your stuff here
// foreach (Award award in awards)

Hope this help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM