反正有没有将 xml 节点值按相似的数字分组?我试图输出如下值,但我无法按照我想要的方式输出。请指导我。谢谢!

我在数据库中的 Answer_Data 示例:(提取后将获得 C1 和 C2)

Question_ID |  Answer_Data
==============================================
    1       | <Answer_Data><Answer>C1</Answer>
    2       | <Answer_Data><Answer>C2</Answer>
    3       | <Answer_Data><Answer>C2</Answer>

使用 Linq 提取后的 String[] 数据:
["c1","c2","c2"]

能够在MVC中查看:
c1
c2
c2

我想要的是:
c1 - 1
c2 - 2

我的 Controller :
public ActionResult SURV_Answer_Result(int Survey_ID, string Language = "ENG")
        {
        List<AnswerQuestionViewModel> viewmodel = new List<AnswerQuestionViewModel>();

                    var query = from r in db.SURV_Question_Ext_Model
                                join s in db.SURV_Question_Model
                                on r.Qext_Question_ID equals
                                s.Question_ID
                                select new { r, s };

                    var viewModel = new AnswerQuestionViewModel();
                    viewModel.Survey_ID = Survey_ID;

                    string[] resultanswer = new string[queryResult.Count()];

                    foreach (var item in query.ToList())
                    {

                        string str = item.s.Answer_Data;
                        XElement qconfig;
                        qconfig = XElement.Parse(str);

                        string value = item.s.Question_Type;
                        int i = 0;

                        switch (value)
                        {

                             case "Choices":
                                {
                                    XElement ChoicesType =
                                    (from node in qconfig.Elements("ChoicesType")
                                     select node).SingleOrDefault();

                                    viewModel.ChoiceType = ChoicesType.Value;

                                    XElement ChoicesAns =
Here is i get the answer data ===>> (from node in qconfig.Elements("Answer")
                                    select node).SingleOrDefault();

                                    resultanswer[i++] = ChoicesAns.Value;
                                    viewModel.ResultAnswer = resultanswer;

                                }
                                break;

                            case "Multiple_Line":
                                {
                                   // do nothing
                                }
                                break;



                        viewmodel.Add(new AnswerQuestionViewModel()
                        {
                            ResultAnswer = viewModel.ResultAnswer
                        });
                    }

                    return View(viewmodel);
                }
        }

我的看法:
 if (Model[i].ChoiceType == "SingleChoice")
               {
                for (int x = 0; x < Model[i].ResultAnswer.Count(); x++)
                {

               @Html.LabelFor(m => m[i].Answer, Model[i].ResultAnswer[x].ToString(),new{ @class="qlabel2" })

                <br/>
            }
       }

最佳答案

正如您所说的那样,您正在接收元素数组,而不是像这样尝试 group by

string[] strarray = new string[] {"c1","c2","c2"};
var groups = from str in strarray
             group str by str into g
             select new {
               key = g.Key,
               count = g.Count()
             }

像这样使用 linq 尝试分组
var xmlstr="<root><Answer_Data><Answer>C1</Answer></Answer_Data>
<Answer_Data><Answer>C2</Answer></Answer_Data>
<Answer_Data><Answer>C2</Answer></Answer_Data></root>";

XDocument xmldoc = XDocument.Parse(xmlstr);

var groups = from record in xmldoc.Descendants("Answer_Data")
             group record by (string)record.Element("Answer")
             into g
             select new {
               key = g.Key,
               count = g.Count()
             }

关于c# - 按相似节点的数量在 MVC View 中显示 XML 节点值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31293929/

10-17 02:03