我正在製作電影數據庫webapp,但我不知道如何使用多對多進行搜索,如果我想要按照流派搜索電影,我該怎麼做?以及如何使實體framwork搜索流派的流派數量,就像我可以搜索1,2或更多的流派。像過濾。實體框架多對多搜索
這是我走到這一步,
var query = from movie in db.Movies where movie.Genres.Any(g => g.GenreID == 2) select movie;
的g.GenreID = 2
目前硬編碼的,但如何使其接受GenreID搜索?我嘗試g => g.GenreID == 2 && g.GenreID == 3
,但它不返回任何電影。當兩個條件都成立時我怎樣才能讓它返回電影?
這是我的桌子,底部是我想要完成的。
Movie
---------------------------
MovieID MovieTitle
1 The GodFather
2 Analyze This
3 The GodFather Part II
Genre
---------------------------
GenreID GenreName
1 Comedy
2 Crime
3 Drama
MovieGenre
---------------------------
MovieID GenreID
1 2 // The GodFather -- Crime, Drama
1 3 //
2 1 // Analyze This -- Comedy, Crime
2 2 //
3 2 // The GodFather Part 2 -- Crime, Drama
3 3 //
Result
---------------------------
Search: Drama
Result: The GodFather, The GodFather Part II
Search: Comedy
Result: Analyze This
Search: Crime
Result: The GodFather, Analyze This, The GodFather Part II
Search: Crime, Drama
Result: Result: The GodFather, The GodFather Part II
Search: Crime, Drama, Comedy
Result: none
這裏是我的模型和上下文
Movie.cs
public class Movie
{
public int MovieID { get; set; }
public string MovieTitle { get; set; }
public virtual ICollection<Genre> Genres { get; set; }
}
Genre.cs
public class Genre
{
public int GenreID { get; set; }
public string GenreName { get; set; }
public virtual ICollection<Movie> Movies { get; set; }
}
AppDbContext.cs
public class AppDbContext : DbContext
{
public AppDbContext() : base("MovieConnection")
{
}
public DbSet<Movie> Movies { get; set; }
public DbSet<Genre> Genres { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
它的工作,但我需要更多的測試。有一些關於var電影的小修改,所以我將它改爲IQueryable電影。謝謝,我一直在尋找幾個小時。 –
markoverflow