[英]How do i merge these queries into 1
So i have below queries. 所以我有以下查询。 I would really like to merge these into 1 query. 我真的很想将这些合并为1个查询。 Next to the fact that this results in an empty collection (which it shouldn't) My brain just imploded when i tried making this into 1 query. 除了这会导致一个空集合(它不应该这样)的事实之外,当我尝试将其放入1个查询时,我的大脑就崩溃了。
So if it's not clear from the code I want to select all horses from a certain user. 因此,如果代码不清楚,我想从特定用户中选择所有赛马。 that have not been signed up for the race that the user is trying to sign up for. 尚未注册的用户尝试参加的比赛。
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();
EDIT 编辑
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;
}
}
Have you considered using the "let" keyword? 您是否考虑过使用“ let”关键字?
http://msdn.microsoft.com/en-us/library/bb383976.aspx http://msdn.microsoft.com/en-us/library/bb383976.aspx
It also seems you could take benefit of the "except" functionality 似乎您也可以利用“除外”功能
http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
(Since I don't have the reputation to make a comment, why are you reusing the variable "id" for both User and identifying a race?) (由于我没有发表评论的声誉,为什么您要同时为User和标识种族使用变量“ id”?)
Additional suggestion is you can filter inside the Single 附加建议是您可以在Single内进行过滤
var race = entities.Races.Single(r => r.Id == raceId);
I am making these assumptions: 我正在做这些假设:
id
, the user ID 您有id
,用户ID raceId
, the ID of the race being signed up for 您有raceId
,正在注册的比赛ID entities.RacingHorses
collection which is the associations of race and horse IDs 有一个entities.RacingHorses
集合,它是种族和赛马ID的关联 Then you could use: 然后,您可以使用:
from h in entities.Horses
where h.UserId == id
let rhs = entities.RacingHorses.Where(rh => rh.HorseId == h.Id)
where !rhs.Any(rh => rh.RaceId == raceId)
select h
Breakdown: 分解:
h
= a user's horse h
=使用者的马 rhs
= RacingHorses associations for h
rhs
= h
RacingHorses协会 raceId
appears in rhs
, then h
is signed up for that race; 如果raceId
出现在rhs
,则h
代表该种族; do not select this h
不要选择这个h
all kudo's go to nmclean for this one. 所有的工藤都去了nmclean。
public static SelectList GetNonRacingHorses(int id, int raceId)
{
HorseTracker entities = new HorseTracker();
var race = entities.Races.Single(r => r.Id == raceId);
var racehorses = from h in race.RacingHorses
select h.Id;
var userhorses = (from h in entities.Horses
where
h.UserId == id
orderby h.Name
select h);
var nonracinghorses = from h in userhorses
where !racehorses.Contains(h.Id)
select h;
List<SelectListItem> sli = new List<SelectListItem>();
foreach (Horse horse in nonracinghorses)
{
sli.Add(new SelectListItem { Text = horse.Name, Value = horse.Id.ToString(), Selected = false});
}
return new SelectList(sli, "Value", "Text");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.