English 中文(简体)
返回值,其中含有条款
原标题:returning values with a where clause

我有两个表格如下:

 ScholarSubject
ScholarSubjectID<pk>
ScholarID
SubjectID
Mark

 AdmissionReq
SubjectID
DegreeCode
MinumumMark

我试图从一个学位表(用PK学位ID)上归还所有东西,在这个表中,学者的标记低于入学最低标记。

public List<object> getDegreeByAPS和Requirements()
    {
        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>();

        }
    }

Everything works, except if I change one of the marks (in ScholarSubject) to a lesser value than the minumum mark (in AdmissionsReq) then it still returns a degree. I want to return a degree if both marks are greater than the minimum requirements 和 not only one of the marks.

我做错什么了 有人能帮帮我吗?

问题回答

我还是不明白你到底想做什么,除非你的数据库里只有一个学者,在我看来,你应该归还一份所有学者取得的所有学位的清单。如果你不想这样做,就需要在 < code> 的 条款中过滤学者ID。

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

  1. 我将更改您的查询, 并以 scollar 而不是 开始, 因为这样可以避免一些重复 :

        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();
    




相关问题
using the where clause + new constraint with args?

I have a piece of code that looks like this: public static T CreateSomething<T>(SomeType a) where T : SomeMicrosoftBaseClass, new() { var newElement = new T { SomeProperty = a}; ...

Strange .Where() behaviour. Somebody has an explanation?

Original I don t get why the Where() clause doesn t give me the right results in the last example. It isn t any different is it? Why does C# behaves differently? transactions = IEnumerable<...

Linq To Sql Where Or operator

I need to create a query which checks if a field (string) contains one or more words supplied at run time. Basically I need to be able to ask a WhereOr question. This seems like it should be a ...

C# Beginner: Where has my IList.Where() method gone?

I ve got another simple one (I think) that s stumping me. I have written a method in one of my controls that gets the latest version of a file in a CMS given it s filename (i.e. regardless of what ...

sql simple query

Yesterday my friend asked me a question about this query: select * from user where 1=1 I said that the query is incorrect, but he said it s correct. I don t understand how this query can be correct....

Codeigniter WHERE on "AS" field

I have a query where I need to modify the selected data and I want to limit my results of that data. For instance: SELECT table_id, radians( 25 ) AS rad FROM test_table WHERE rad < 5 ORDER BY rad ...

SEARCH several Tables IN SQL

I m trying to search several tables at once for a search term. My query is: SELECT item.ItemID FROM Inventory.Item item JOIN Inventory.Category catR // each item can be in several ...