Many to Many Relationship Filter/Search

asp.net-core c# entity-framework-core

Question

Hello i want filtering to LanguageSchool table but LanguageSchool table has relation with LanguageSchoolProgram table(many to many).

public class LanguageSchool
{
    public virtual List<LanguageSchoolLanguageSchoolProgram> LanguageSchoolLanguageSchoolPrograms { get; set; }
}

public class LanguageSchoolProgram
{
     public long Id { get; set; }
     public virtual List<LanguageSchoolLanguageSchoolProgram> LanguageSchoolLanguageSchoolPrograms { get; set; }
}
public class LanguageSchoolLanguageSchoolProgram
{
    public long LanguageSchoolProgramId { get; set; }
    public virtual LanguageSchoolProgram LanguageSchoolProgram { get; set; }
    public long LanguageSchoolId { get; set; }
    public virtual LanguageSchool LanguageSchool { get; set; }
}

i have X Language School Program, Y Language School Program and Z Language Program and a lot of language school.

I want to select language schools but Language schools has containing X and Y program.

and i must use .AsQueryable() function.

1
1
12/8/2018 3:45:19 AM

Popular Answer

Your question is not clear... I am not sure how you store programs X, Y, Z? I am assuming these are program names.

You can defined your many to many relationship like this:

public class LanguageSchool
{
    public int LanguageSchoolId { get; set; }
    public virtual ICollection<LanguageSchoolProgram> LanguageSchoolPrograms { get; set; }
}

public class LanguageSchoolProgram
{
    public int LanguageSchoolProgramId { get; set; }
    public string ProgramName {get; set;} // this is X, Y or Z
    public virtual ICollection<LanguageSchool> LanguageSchools { get; set; }
}

And use Contains to select schools with programs in the list:

string[] requiredPrograms = new string[] {"X", "Y", "X"};
var schoolsQueryable = dbContext.LanguageSchool.Where(
    ls => requiredPrograms.contains(ls.LanguageSchoolProgram.ProgramName));

// if you don't want Queryable, then add ToList()
0
12/11/2018 9:37:24 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow