2011-02-24 44 views
1

嗨 我想創建一個這樣的表的查詢: 1.i有一個多重的問題,我想統計每個響應的數量每個回答多個問題的結果,並將它們顯示在如下格式的查詢中: 1.ID 2.問題短語3.q1 4.q2 5.q3 6.q4 7.count1 8.count2 9.count3 10.count4 i已經創建了一個LINQ查詢像這樣的是答案表之後,我可以用ID來問表加入這些並獲得短語等:如何使一個linq查詢像一個平坦的表

var q4 = (from x in LinqDB.PTAs 
      where x.PTID == int.Parse(DropDownListPeriodID.SelectedValue) && 
        x.PTUser.PTUserID >= ID1 && x.PTUser.PTUserID <= ID2 
      group x by x.PTQID into GRPA //& x.PTAID 
      select new { 
       GRPA.Key, 
       A1=(
        from f in GRPA 
        group f by f.Answer into FG 
        select FG.Count() 
        )}); 

但它給了我不的第二層w^

var Q = from x in LinqDB.PTAs 
      where x.PTID == int.Parse(DropDownListPeriodID.SelectedValue) && 
        x.PTUser.PTUserID >= ID1 && x.PTUser.PTUserID <= ID2 
      group x by new { x.PTQID, x.Answer } into gr 
      select new {gr.Key.PTQID,gr.Key.Answer,A1=gr.Count() }; 

因此任何建議,這個或可能改變第二查詢在後處理中,因此它可以像我的格式:格式 我想這螞蟻becouse? Thx爲您的答案。

回答

0
//process user input before going into the database. 
int selectedID = int.Parse(DropDownListPeriodID.SelectedValue); 

    //go to the database and get the data. 
List<PTA> records = 
(
    from x in LinqDB.PTAs 
    where x.PTID == selectedID 
    && ID1 <= x.PTUser.PTUserID && x.PTUser.PTUserID <= ID2 
    select x 
).ToList(); 

// group up the data for presentation.  
var questions = 
    from x in records 
    group x by new {x.PTQID, x.Answer} into g1 
    group g1 by g1.Key.PTQID into g2 
    select new 
    { 
    PTQID = g2.Key, 
    Answers = 
     from y in g2 
     select new 
     { 
     Answer = y.Key.Answer, 
     AnswerCount = y.Count() 
     } 
    }; 

// group up the data for presentation. 
    var questions = 
    from x in records 
    group x by x.PTQID into g 
    let answers = g.GroupBy(y => y.Answer) 
    let answer1 = answers.First() 
    let answer2 = answers.Skip(1).First() 
    let answer3 = answers.Skip(2).First() 
    let answer4 = answers.Skip(3).First() 
    select new 
    { 
    PTQID = g.Key, 
    Answer1 = answer1.Key, 
    Answer1Count = answer1.Count(), 
    Answer2 = answer1.Key, 
    Answer2Count = answer1.Count(), 
    Answer3 = answer1.Key, 
    Answer3Count = answer1.Count(), 
    Answer4 = answer1.Key, 
    Answer4Count = answer1.Count(), 
    }; 
+0

您好大衛乙感謝您的答覆。但你發送的查詢給出了3層不是1.i說,我希望它是這樣的格式因爲我想發送這個在水晶報告源,它不能識別超過1 layer.i上傳的圖像第一個查詢是2層,第二個查詢是1層。 – user629436 2011-02-25 04:58:53

+0

http://imgbot.com/images/59318563781857354819.jpg http://imgbot.com/images/71394487961413797036.jpg – user629436 2011-02-25 04:59:37

+0

它應該像第二個單獨的圖像,一切都在一張平坦的桌子上,就像結果一樣,所以它應該只是在調試模式下給出一個RESULT VIEW,並且不會再有。非常多的thx。 – user629436 2011-02-25 05:02:50