Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
How to implement searching with Include in ASP.NET CORE

I have two models in my project. first for Movie and second for Genre. There is a many-to-many relation between the two. So I created another model named MovieGenre. The problem is I don't know how to implement a search function in the controller for name of the Genre. To retrieve a movie with the specified genre name I mean.

Here is the three models

public class Movie
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<MovieGenre> MovieGenres { get; set; }
        public string Director { get; set; }
        public DateTime ReleaseDate { get; set; } = DateTime.Now;

    }

public class Genre
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<MovieGenre> MovieGenres { get; set; }
    }

public class MovieGenre
    {
        public int MovieId { get; set; }
        public int GenreId { get; set; }
        public Movie Movie { get; set; }
        public Genre Genre { get; set; }
    }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This query has nothing with Include but how implement filter

var result = ctx.Movie
    .Where(m => m.MovieGenres.Any(mg => mg.Genre.Name == genreName))
    .ToList();

If you are using EF Core 5+, consider to simplify your model. Intermediate entity can be omitted/hidden, check documentation:

public class Movie
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Genre> Genres { get; set; }
    public string Director { get; set; }
    public DateTime ReleaseDate { get; set; } = DateTime.Now;
}

public class Genre
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Movie> Movies { get; set; }
}
var result = ctx.Movie
    .Where(m => m.Genres.Any(g => g.Name == genreName))
    .ToList();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!