我为“asp.net核心mvc 3.0”网站制作过滤器。我有以下过滤器:“语言”、“专业”、“成本”。我可以从网站上得到所有过滤器的ID。但我不能返回到符合这些过滤器的网站用户。下面是我的代码:
类用户
public class User : IdentityUser
{
public virtual ICollection<UserAndLanguage> UserAndLanguages { get; set; } = new List<UserAndLanguage>();
public virtual ICollection<TutorAndProfession> andProfessions { get; set; } = new List<TutorAndProfession>();
//public List<UserAndLanguage> andLanguages { get; set; }
public virtual ICollection<TutorProfessionAndCost> TutorProfessionAndCosts { get; set; } = new List<TutorProfessionAndCost>();
[Not Mapped]
public List<Language> Languages { get; set; }
[Not Mapped]
public List<TutorProfessionAndCost> ProfessionAndCosts { get; set; }
public User()
{
Languages = new List<Language>();
ProfessionAndCosts = new List<TutorProfessionAndCost>();
}
}
class TutorProfessionAndCost
public class TutorProfessionAndCost
{
public int Id { get; set; }
public string UserId { get; set; }
[Not Mapped]
public List<User> Users { get; set; }
public TutorProfessionAndCost()
{
Users = new List<User>();
}
//profession
public int IdTutorProfession { get; set; }
public string EducationalSubject { get; set; }
public int Cost_One { get; set; } //the cost of the lesson is individual
public int Cost_Group { get; set; } //cost of class group class
public virtual ICollection<TutorAndProfession> UserAndProfs { get; set; } = new List<TutorAndProfession>();
}
UserAndLanguage类
public class UserAndLanguage
{
public int Id { get; set; }
public string UserId { get; set; }
public User User { get; set; }
public int LangId { get; set; }
public Language Language { get; set; }
}
数据库:
public DbSet<UserAndLanguage> UserAndLanguages { get; set; }
public DbSet<TutorAndProfession> TutorAndProfessions { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<User>().HasKey(u => u.Id);
//many-to-many communication for the languages in which the user teaches
builder.Entity<UserAndLanguage>()
.HasKey(ul => new { ul.UserId, ul.LangId });
builder.Entity<UserAndLanguage>()
.HasOne(ul => ul.User)
.WithMany(u => u.UserAndLanguages)
.HasForeignKey(ul => ul.UserId);
builder.Entity<UserAndLanguage>()
.HasOne(ul => ul.Language)
.WithMany(u => u.UserAndLanguages)
.HasForeignKey(ul => ul.LangId);
//connection for professions and user
//builder.Entity<TutorProfessionAndCost>()
//.HasOne(p => p.User)
//.WithMany(u => u.ProfessionAndCosts)
//.HasForeignKey(p => p.UserId);
//many-to-many relationship for professions and user
builder.Entity<TutorAndProfession>()
.HasKey(up => new { up.UserId, up.ProfessionId });
builder.Entity<TutorAndProfession>()
.HasOne(up => up.User)
.WithMany(u => u.andProfessions)
.HasForeignKey(up => up.UserId);
builder.Entity<TutorAndProfession>()
.HasOne(up => up.Profession)
.WithMany(u => u.UserAndProfs)
.HasForeignKey(up => up.ProfessionId);
}
控制器
public IActionResult TutorsInfo(int ProfesionTutor, int LanguageTutor, string OneOrGroupTutor, int PriseTutor)
{
//List<User> Tutors = new List<User>();
List<UserAndLanguage> tLang = new List<UserAndLanguage>();
//List<string> tLang = new List<string>();
if (LanguageTutor == 0)
tLang = _context.UserAndLanguages.ToList();
else
tLang = _context.UserAndLanguages.Where(t => t.LangId == LanguageTutor).ToList();
List<TutorProfesionAndCost> tProf = new List<TutorProfesionAndCost>();
if(PriseTutor != 0)
{
if (OneOrGroupTutor == "all")
tProf = _context.TutorProfesionAndCosts.Where(t => t.IdTutorProfesion == ProfesionTutor).ToList();
else if (OneOrGroupTutor == "One")
tProf = _context.TutorProfesionAndCosts.Where(t => t.IdTutorProfesion == ProfesionTutor).Where(t => t.Cost_One != 0 && t.Cost_One <= PriseTutor).ToList();
else if (OneOrGroupTutor == "Group")
tProf = _context.TutorProfesionAndCosts.Where(t => t.IdTutorProfesion == ProfesionTutor).Where(t => t.Cost_Group != 0 && t.Cost_Group <= PriseTutor).ToList();
else if (OneOrGroupTutor == "Work")
tProf = _context.TutorProfesionAndCosts.Where(t => t.IdTutorProfesion == ProfesionTutor).Where(t => t.ControlWork == true).ToList();
}
else
{
if (OneOrGroupTutor == "all")
tProf = _context.TutorProfesionAndCosts.Where(t => t.IdTutorProfesion == ProfesionTutor).ToList();
else if (OneOrGroupTutor == "One")
tProf = _context.TutorProfesionAndCosts.Where(t => t.IdTutorProfesion == ProfesionTutor).Where(t => t.Cost_One != 0).ToList();
else if (OneOrGroupTutor == "Group")
tProf = _context.TutorProfesionAndCosts.Where(t => t.IdTutorProfesion == ProfesionTutor).Where(t => t.Cost_Group != 0).ToList();
else if (OneOrGroupTutor == "Work")
tProf = _context.TutorProfesionAndCosts.Where(t => t.IdTutorProfesion == ProfesionTutor).Where(t => t.ControlWork == true).ToList();
}
if (tProf == null && ProfesionTutor == 0)
tProf = _context.TutorProfesionAndCosts.ToList();
//tProf not null
//tLang not null
List<User> tut = _context.Users
.Where(u => u.TutorProfesionAndCosts == tProf)
.Where(u => u.UserAndLanguages == tLang)
.ToList();
//error. tut = null
return View(tut);
}
1条答案
按热度按时间plicqrtu1#
问题解决了。重写了控制器代码: