0

我有两个表如下:

 ScholarSubject
ScholarSubjectID<pk>
ScholarID
SubjectID
Mark

 AdmissionReq
SubjectID
DegreeCode
MinumumMark

我正在尝试从学位表(带有 PK degreeID)中返回所有内容,其中学者的分数低于录取的最低分数。我的查询如下:

public List<object> getDegreeByAPSandRequirements()
    {
        using (DataLayer.CareerDatabaseEntities context = new DataLayer.CareerDatabaseEntities())
        {
            return (from Degrees in context.Degrees
                    join admissions in context.AdmissionReqs on
                    Degrees.DegreeCode equals admissions.DegreeCode
                    join subject in context.Subjects on
                    admissions.SubjectID equals subject.SubjectID
                    join scholarsubject in context.ScholarSubjects on
                    subject.SubjectID equals scholarsubject.SubjectID
                    join scholar in context.Scholars on
                    scholarsubject.ScholarID equals scholar.ScholarID
                    where Degrees.APSScore <= scholar.APSScore && admissions.MinimumMark <= scholarsubject.NSC && scholarsubject.SubjectID.Equals(admissions.SubjectID)
                    select Degrees).Distinct().ToList<object>();

        }
    }

一切正常,除非我将其中一个分数(在 ScholarSubject 中)更改为比最低分数(在 AdmissionsReq 中)更小的值,那么它仍然会返回一个学位。如果两个分数都高于最低要求,而不仅仅是一个分数,我想返回学位。

我究竟做错了什么?有人可以帮帮我吗??

4

1 回答 1

0

我仍然不确定我是否理解您要做什么-除非您的数据库中只有一位学者,否则在我看来,您将返回所有学者获得的所有学位的列表。如果你不想要这个,你需要在where子句中过滤scholarID。

但无论如何 - 为了获得更多信息,我会尝试做两件事。

  1. 我会更改您的查询并以scholarnot开头,degree因为这可能会避免一些重复:

        from scholar in context.Scholars 
        join scholarsubject in context.ScholarSubjects on scholar.ScholarID equals scholarsubject.ScholarID
        join subject in context.Subjects on scholarsubject.SubjectID equals subject.SubjectID
        join admission in context.AdmissionReqs on subject.SubjectID equals admission.SubjectID
        join degree in context.Degrees on admission.DegreeCode equals degree.DegreeCode
        where degree.APSScore <= scholar.APSScore 
            && admission.MinimumMark <= scholarsubject.NSC 
          //&& scholarsubject.SubjectID.Equals(admission.SubjectID) // you should not need this line as you have the joins in place to assert this
        select degree)
       .Distinct()
       .ToList<object>();
    
  2. 如果这产生与您之前的查询相同的结果,那么我将更改返回类型以查看您到底得到了什么 - 用这个替换最后一行并检查集合:

     select new {ScholarID = scholar.ScholarID, Degree = degree})
       .Distinct()
       .ToList();
    
于 2012-05-21T19:18:23.727 回答