大家好日子,
我有2个模型,彼此之间有关系。一对多的关系
// Main model
public class MainDetails {
[Key]
public int MainDetailId { get; set; }
public string Name { get; set; }
public ICollection<SubDetails> SubDetails { get; set; }
}
// Sub model
public class SubDetails {
[Key]
public int SubDetailId{ get; set; }
public string School { get; set; }
public MainDetails MainDetails { get; set; }
public int MainDetailId { get; set; }
}
现在这里是我的代码,我遇到了问题。
public IQuerable<MainDetails> SearchDetails(string school){
return context.MainDetails.Where(details=>details.SubDetails.School == school)
// details.SubDetails.School is an ICollection so this not working.
}
public IQuerable<MainDetails> SearchDetails(string school){
return context.MainDetails.Inclue(x=>x.SubDetails).Where(details=>details.SubDetails.School == school)
// This doesn't work too
}
为什么我无法获得SubDetails.School
?还是要解决这个问题?
对不起,我在谈到关系时是新的。我希望你能和我分享一些。谢谢!
答案 0 :(得分:2)
这段代码甚至不应该编译......
public IQuerable<MainDetails> SearchDetails(string school)
{
return context.MainDetails.Where(details=>details.SubDetails.School == school)
// details.SubDetails.School is an ICollection so this not working.
}
你应该......
public IQuerable<MainDetails> SearchDetails(string school)
{
return context
.MainDetails
.Where(details=>
details.SubDetails.Any(sd => sd.School == school));
}