2

だから私は以下の質問があります。これらを 1 つのクエリにマージしたいと思います。これが空のコレクションになるという事実に加えて(そうすべきではありません)、これを1つのクエリにしようとしたとき、私の脳は崩壊しました。

したがって、コードから明らかでない場合は、特定のユーザーからすべての馬を選択したいと考えています。ユーザーがサインアップしようとしているレースにサインアップしていない。

var userhorses = (from h in entities.Horses
                          where h.UserId == id
                          select h);

var race = (from r in entities.Races
                    where r.Id == id
                    select r).Single();

var runninghorses = (from rh in race.RacingHorses
                     where rh.UserId == id
                     select rh);

var nonracinghorses = (from nrh in userhorses
                       from rh in runninghorses
                       where nrh.Id != rh.Id
                       select nrh).ToList();

編集

public class Horse
{
    [Key]
    public int Id { get; set; }

    public int? UserId { get; set; }
    public virtual User Owner { get; set; }

    [Required(ErrorMessage = "Name is required")]
    public string Name { get; set; }
    public int? GenderId { get; set; }
    public virtual Gender Gender { get; set; }
    public int? ColorId { get; set; }
    public virtual Color Color { get; set; }
    public int? LegTypeId { get; set; }
    public virtual LegType LegType { get; set; }
    public int? CharacterId { get; set; }
    public virtual Character Character { get; set; }
    public int Hearts { get; set; }
    public bool Retired { get; set; }
    public bool CPU { get; set; }
    public bool ForSale { get; set; }
    public int ListPrice { get; set; }
    public DateTime? Deadline { get; set; }
    // Parents
    public int? SireId { get; set; }
    public virtual Horse Sire { get; set; }
    public int? DamId { get; set; }
    public virtual Horse Dam { get; set; }
    // Internals
    public int Stamina { get; set; }
    public int Speed { get; set; }
    public int Sharp { get; set; }
    // Special
    public int Dirt { get; set; }
    // Externals
    public int Start { get; set; }
    public int Corner { get; set; }
    public int OutOfTheBox { get; set; }
    public int Competing { get; set; }
    public int Tenacious { get; set; }
    public int Spurt { get; set; }
    //Future
    public virtual ICollection<Race> FutureRaces { get; set; }
    //RaceResults
    public virtual ICollection<RaceResult> RaceResults { get; set; }
    //Training
    public virtual ICollection<Training> TrainingResults { get; set; }
    //Bids
    public virtual ICollection<Bid> Bids { get; set; }

    public Horse() {
        ForSale = false; //default value
        Deadline = null;
    }
}


public class Race
{
    [Key]
    public int Id { get; set; }
    [Required(ErrorMessage = "Name is required")]
    public string Name { get; set; }
    [Required(ErrorMessage = "Purse is required")]
    public int Purse { get; set; }
    [Required(ErrorMessage = "Slots are required")]
    public int Slots { get; set; }
    public int SlotPrice { get; set; }
    public DateTime? RaceTime { get; set; }

    //public int? TrackId { get; set; }
    //public virtual Track Track { get; set; }

    public int? OwnerId { get; set; }
    public virtual User Owner { get; set; }

    public virtual ICollection<Horse> RacingHorses { get; set; }

    public virtual ICollection<RaceResult> RaceResults { get; set; }

    public Race()
    {
        SlotPrice = 0; //default value
        Slots = 8;


    }


}
4

3 に答える 3