我想对LINQ的问题和答案进行分组,并使其正确显示

时间:2018-10-07 02:17:20

标签: linq html-table

我正在尝试适当地分组以使我的牢房在晚上分配。打印输出非常奇怪和不均匀。

是我正在创建的表还是对表进行分组或同时对两者进行分组?我认为这个小组是正确的。我的结果如下图所示。

黄金是标题, 绿色是问题, 红色是答案

enter image description here

enter image description here

Mt表在下面

var Sections = new OncologySection().SelectSections(projectID.ToString());
        int iSection = 0;
        int igroups = 0;
        int ianswer = 0;

        tb.CssClass = "";
        tb.BorderWidth = 1;
        tb.Width = new Unit("780px");
        tb.Attributes.Add("runat", "server");

        foreach (OncologySection section in Sections)
        {
            TableRow row1 = new TableRow();
            iSection++;
                            // var getDistinctQuestion = getVoterAnswerstoList.Select(s => s.QuestionText ,s.Id).Distinct().ToList();

            var getVoterAnswerstoList = new OncologyGeneratePDFDAL().DataforPDFCreation(Convert.ToInt32(projectID), Convert.ToInt32(voterid), Convert.ToInt32(caseId), Convert.ToInt32(voteSurveyId), Convert.ToInt32(section.SectionID)).OrderBy(os => os.SortOrder);

            //var groupedCustomerList = getVoterAnswerstoList
            //    .GroupBy(u => u.QuestionText, u.QuestionText)
            //    .Select(grp => grp.ToList())
            //    .ToList();

            var groupedCustomerList = getVoterAnswerstoList.GroupBy(x => new { x.QuestionText, x.DynamicValue }).ToList();


            TableCell cell1 = new TableCell();
            cell1.BorderWidth = 1;
            cell1.Text = section.SectionName;
            cell1.BorderColor = System.Drawing.Color.Goldenrod;
            cell1.ColumnSpan = groupedCustomerList.Count();
            row1.Cells.Add(cell1);
            tb.Rows.Add(row1);


            TableRow row2 = new TableRow();

            foreach (var groups in groupedCustomerList)
            {
                igroups++;

                TableCell cell2 = new TableCell();

                var q = (from s in groups select s.QuestionText).FirstOrDefault();
                cell2.BorderWidth = 1;
                cell2.Text = q;
                cell2.BorderColor = System.Drawing.Color.Green;
                cell2.ColumnSpan = groupedCustomerList.Count();
                row2.Cells.Add(cell2);


                if (igroups == groupedCustomerList.Count())
                {
                    tb.Rows.Add(row2);
                }
                else
                {
                    row2.Cells.Add(cell2);
                }
                TableRow row3 = new TableRow();
                foreach (var answers in groups)
                {

                    ianswer++;

                    TableCell cell3 = new TableCell();

                    cell3.BorderWidth = 1;

                    cell3.BorderColor = System.Drawing.Color.DarkRed;
                    if (answers.DataTypeId == 7)
                    {
                        cell3.Text = answers.DynamicValue.ToString();
                    }
                    else if ((answers.DataTypeId == 5) || (answers.DataTypeId == 6) || (answers.DataTypeId == 8))
                    {

                        if (answers.VotingValue != 0)
                        {
                            cell3.Text = answers.VotingValue.ToString();
                        }
                        else
                        {
                            cell3.Text = " ";
                        }
                    }
                    else
                    {

                        cell3.Text = " ";
                    }

                    row3.Cells.Add(cell3);
                    tb.Rows.Add(row3);
                }

            }

        } 

    }

0 个答案:

没有答案