所以我有以下查询。我真的很想将这些合并到 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;
}
}