Entity Framework – Querying Child Collections in LINQ
Team.cs |
[Table("TEAM")] public partial class TEAM { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public int TEAM_ID { get; set; } [Required] public string TEAM_NAME { get; set; } public int TEAM_LEAGUE { get; set; } public int TEAM_CITY { get; set; } public int TEAM_STADIUM { get; set; } … … [… …] public virtual ICollection PLAYERs { get; set; } … … } |
Player.cs |
[Table("PLAYER")] public partial class PLAYER { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public int PLAYER_ID { get; set; } [Required] public string PLAYER_NAME { get; set; } public int PLAYER_POSITION { get; set; } public int PLAYER_NATION { get; set; } public int PLAYER_TEAM { get; set; } … … } |
PlayerService.cs |
using System; using System.Collections.Generic; using System.Linq; using SoccerDAO; public List<TEAM> getTeamByPlayerName(string Name) { List<TEAM> list = this.context.TEAMs.Where(t => t.PLAYERs.Any(p => p.PLAYER_NAME.Contains(Name))).ToList(); return list; } |