开发者

How to make a groupped linq Query like a flat table

Hi i want to create a query for a table that is like this: 1.i have a multiple question and i want to count the number of each response to every answer to multiplequestion result and show them in a query that is formated like this: 1.ID 2.qestion phrase 3.q1 4.q2 5.q3 6.q4 7.count1 8.count2 9.count3 10.count4 i have created a linq query like this that is from answers table and after that i can join these with the ID to questions table and get the phrase and other:

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()
                    )})开发者_开发技巧;

but it gives a second layer that i don't want becouse of the format i tried this:

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

so any suggestions to this or maybe changing the second query in a post processing so it can be like my format? Thx for your Answers.


  //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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜