在LINQ中获取外键表的所有列以及主表列

时间:2016-03-23 05:05:52

标签: c# asp.net-mvc linq

我正在尝试为所有学生(学生列表中的主题列表)获取所有科目的列表。

主题表(SubjectPk,StudentId,SubjectName,Description,AddedOn,AddedBy)为单个学生提供了许多主题。

我试过这个:没有运气

<VirtualHost *:80>
        ServerName sub.website.com
        ServerAlias www.sub.website.com sub.website.com
        VirtualDocumentRoot /home/website/public_html
        <Directory /home/website/public_html>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
        LimitInternalRecursion 20
        #set logging
        ErrorLog ${APACHE_LOG_DIR}/error.log
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel notice
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        SetEnv APPLICATION_ENV "development"
#       SuexecUserGroup %1 %1

<IfModule mod_fastcgi.c>
    AddHandler php5-fcgi .php
    Action php5-fcgi /php5-fcgi
    Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi-website
#    FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -host 127.0.0.1:9000 -idle-timeout 900 -pass-header Authorization
    FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi-website -socket /var/run/website-php5-fpm.sock -idle-timeout 900 -pass-header Authorization

</IfModule>

</VirtualHost>

而且:没有运气

     var data = (from st in db.Student                     
                       join sub in db.Subject on st.StudentId equals  sub.st.StudentId into subjectsList

                       select new StudentModel
                       {
                           Name = st.Name,
               Class= st.Class,
               RollNo = st.RollNo,
                           SubjectList = subjectsList //public ILIst<Subject> SubjectList {get;set;}
                       }).ToList();

var data = (from st in db.Student                     
                       select new StudentModel
                       {
                           Name = st.Name,
               Class= st.Class,
               RollNo = st.RollNo,
                           SubjectList = db.Subject.where(s.StudentId == st.StudentId).ToList()

                       }).ToList();

3 个答案:

答案 0 :(得分:0)

由于这看起来像学生和主题之间的多对多关系,理想情况下,您应该有一个不同的表来引用学生和主题。所以你应该有这样的东西:

public class Student{
   public int Id{get; set;}
   public string StudentName{get; set;}
   public IList<StudentsSubjects> Subjects{get; set;}
   // other properties
}

public class Subject{
   public int Id{get; set;}
   public string SubjectTitle{get; set;}
   public IList<StudentsSubjects> Students{get; set;}
   // other properties
}

public class StudentsSubjects{
  public int Id{get; set;}
  public int StudentId{get; set;}
  public int SubjectId{get; set;}
  public DateTime DateAdded{get; set;}
}

然后,如果您正在使用实体框架,那么

data = db.Student.Include(s => s.Subjects).ToList()

然后使用foreach循环并获取所需的数据。

答案 1 :(得分:0)

你的第一次尝试看起来没问题,只是我将sub.st.StudentId删除为sub.StudentId

 var data = (from st in db.Student                     
                   join sub in db.Subject on st.StudentId equals 
                   sub.StudentId into subjectsList

                   select new StudentModel
                   {
                       Name = st.Name,
           Class= st.Class,
           RollNo = st.RollNo,
                       SubjectList = subjectsList //public ILIst<Subject> SubjectList {get;set;}
                   }).ToList();

答案 2 :(得分:0)

也许你可以为subjectList添加ToList()。请试试这个:

 var data = (from st in db.Student                     
             join sub in db.Subject on st.StudentId equals  sub.st.StudentId into subjectsList
             select new StudentModel
             {
                 Name = st.Name,
                 Class= st.Class,
                 RollNo = st.RollNo,
                 SubjectList = subjectsList.ToList() //public ILIst<Subject> SubjectList {get;set;}
             }).ToList();