SQL到linq的多对多关系(C#ASP.net MVC)
我正在寻找一些帮助,以便能够从SQL查询中转录一些LINQ: 以下是我的数据库的快速查看: 实体框架“简化”我的“etudiant”模型如下:SQL到linq的多对多关系(C#ASP.net MVC),c#,entity-framework,linq-to-entities,C#,Entity Framework,Linq To Entities,我正在寻找一些帮助,以便能够从SQL查询中转录一些LINQ: 以下是我的数据库的快速查看: 实体框架“简化”我的“etudiant”模型如下: [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<etuResult&g
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<etuResult> etuResult { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<cours> cours { get; set; }
select c.libelle
from cours c
where c.cours_id in (
select ec.cours_id
from etuCours ec
where ec.etudiant_id in (
select e.etudiant_id
from etudiant e
))
但是我找不到如何使用linq进行查询(我使用LinqPad 4进行测试)
提前感谢您的帮助为什么不直接给实体打电话?如果已映射,则会自动进行过滤。无需创建另一个查询
var courses = myEtudiantInstance.cours;
如果你想要诽谤罪,那么
var libelles = myEtudiantInstance.cours.Select(c => c.libelle);
这需要启用延迟加载,或者在获取集合上的Etudiant
实例时使用Include
var libelles = dbContextInstance.Etudiants
.Include(e => e.cours)
.Single(e => e.EtudiantId == 1) // will throw exception if entity not found
.cours.Select(c => c.libelle); // get all libelle's
编辑:如果这对某人有用: 使用DAL文件夹(用于数据访问层)更简单,如教程所示(在副标题“创建数据库上下文”下) 如果你不明白这个文件夹的用途,我建议你链接
如果我改进了我的MVC并遵循本教程,我就不会因为我的Linq查询而遇到麻烦了我不知道你是如何做到的,但你解决了我的问题!谢谢,我将继续学习这门语言:)