0

我有模特

public class GrandPrix
{
    public int Id { get; set; }

    [Required]
    [Display(Name = "Имя")]
    [DataType(DataType.Text)]
    public string Name { get; set; }

    public int LocationId { get; set; }

    public int? ChampionchipId { get; set; }

    public virtual Location Location { get; set; }
    public virtual Championchip Championchip { get; set; }
}
    public class Location
{
    public int Id { get; set; }

    [Required]
    [Display(Name = "Location")]
    [DataType(DataType.Text)]
    public string Name { get; set; }

//    public virtual ICollection<GrandPrix> GrandPrix { get; set; } 
}

我需要修改

ViewBag.LocationId = new SelectList(db.Locations, "Id", "Name");

db.Locations .Where(l=>l.id 不包含在 db.Grandprix.Where(g=>g.ChampionchipId == 1))

这个查询它工作

SELECT Locations.Id as Id, Locations.Name as Name FROM dbo.Locations WHERE Locations.Id Not In (SELECT GrandPrixes.LocationId FROM GrandPrixes WHERE ChampionchipId = 1)

对不起我的英语,谢谢你的帮助。

4

1 回答 1

1
db.Locations.Where(loc=>loc.GrandPrixes.Any(gp=>gp.ChampionshipId != 1))

如果您的 FK 关系设置正确,则应该为您提供位置。

别的

var gps = db.GrandPrixes.Where(gp=>gp.ChampionshipId == 1)
                        .Select(gp=>gp.Location.LocationId).ToList();
db.Locations.Where(loc=>!gps.Any(gp=>gp == loc.LocationId))
于 2012-10-15T01:56:13.690 回答