2013-10-24 65 views
0

請讓我有一個項目,我使用實體框架查詢數據庫作爲我的ORM。這是我的查詢如何基於特定的gridview列在gridview上創建虛擬列

Dim subjectSearch = From subSearch In DemoSchool.EssenceSubjectRegistrations Where subSearch.Session = drpSession.SelectedItem.Text _ 
       AndAlso subSearch.Term = drpTerm.SelectedItem.Text AndAlso subSearch.RealClass.ClassSN = findClassSN.FirstOrDefault AndAlso _ 
        subSearch.SubjectCode = drpSubject.SelectedValue _ 
        Select New With {.SubjectRegSN = subSearch.SubjectRegSN, 
            .FirstName = subSearch.Student.FirstName, 
            .Surname = subSearch.Student.Surname, 
            .CA1 = subSearch.CA1, 
            .CA2 = subSearch.CA2, 
            .CA3 = subSearch.CA3, 
            .CA4 = subSearch.CA4, 
            .CA5 = subSearch.CA5} 

然後我查詢我的結果,這樣我可以能夠通過這樣

Dim secSubjectSearch = (From jamie In subjectSearch Select jamie).ToList() _ 
         .Select(Function(jamie) New With {.SubjectRegSN = jamie.SubjectRegSN, 
         .FirstName = jamie.FirstName, 
         .Surname = jamie.Surname, 
         .CA1 = jamie.CA1, 
         .CA2 = jamie.CA2, 
         .CA3 = jamie.CA3, 
         .CA4 = jamie.CA4, 
         .CA5 = jamie.CA5, 
         .MidTerm = CDbl(jamie.CA1 + jamie.CA2 + jamie.CA3 + jamie.CA4 + jamie.CA5)/5}) 

第二查詢的結果做一些關於它的操作是有界到GridView哪些按原樣呈現正確渲染。我的問題是我想要在.MidTerm有界列之後的gridview上創建一個名爲Rank的虛擬列,它將在搜索結果中顯示每個人記錄的位置。

NAME  CA1  CA2  CA3 CA4  CA5 MIDTERM RANK 
    James  50  50  60  40  60  52  3 
    Essty  100  50  50  50  50  60  2 
    Markus  100  40  50  60  50  60  2 
    Code  100  100  100  100  50  90  1 

以上是gridview列的格式。我希望列的排名是一個虛擬的列,根據學生的中期分數計算。代碼VB得分90,所以他的排名等於1等。請我不知道如何計算這個排名列和代碼得到最高和最小。真的我需要幫助謝謝

回答

0

兩種方式。

  1. 使用綁定列和計算/顯示排名

  2. 有一個RANK場和預先計算它,約束它,像:

     Dim secSubjectSearch = (From jamie In subjectSearch Select jamie).ToList() _ 
           .Select(Function(jamie) New With {.SubjectRegSN = jamie.SubjectRegSN, 
           .FirstName = jamie.FirstName, 
           .Surname = jamie.Surname, 
           .CA1 = jamie.CA1, 
           .CA2 = jamie.CA2, 
           .CA3 = jamie.CA3, 
           .CA4 = jamie.CA4, 
           .CA5 = jamie.CA5, 
           .MidTerm = CDbl(jamie.CA1 + jamie.CA2 + jamie.CA3 + jamie.CA4 + jamie.CA5)/5, 
           .RANK = -1}).ToList() 
         dim sorted = secSubjectSearch.Select(function(n) n.MidTerm).Distinct().OrderByDescending(function(n) n).ToList() 
         for each itm in secSubjectSearch 
          itm.RANK = sorted.IndexOf(itm.MidTerm) + 1 
         next 
    

- 不測試,用記事本寫它...

+0

工作就像一個魅力。請你可以向我解釋爲什麼你的代碼工作,以便我可以學習。謝謝 –

+0

沒有什麼棘手的 - 只需按降序將所有不同的MidTerm值存儲在列表中,然後爲每個學生找到正確的RANK,方法是在排序列表中找到MidTerm的索引,然後再加上一個。當然,我們必須爲這個RANK信息創建一個佔位符:做到這一點,只需將一個屬性RANK的默認值爲-1(該值無關緊要,因爲您稍後將更新它) – Rex