2017-11-03 5 views
1

は、私のようにJSONデータを持って考えてみて、同じキーと値のペアを持つ2つのデータ:あなたは、私が利用可能と各質問のリストを持って見ることができるようにJSON - オブジェクトまたは配列のためのLINQクエリグループへ

{ 
"entities":[ 
    { 
     "republish": false, 
     "OrgID": "", 
     "createdby": "730", 
     "questions": [ 
     { 
      "sequence": "5", 
      "QuestionId": "57BB6DDC-A90A-10EE-E224-EC658A825871", 
      "metadata": [ 
      { 
       "key": "Group", 
       "value": 0 
      }, 
      { 
       "key": "Part", 
       "value": "0" 
      } 
      ] 
     }, 
     { 
      "sequence": "4", 
      "QuestionId": "57BB6DDC-A90A-10EE-E224-EC658A825871", 
      "metadata": [ 
      { 
       "key": "Group", 
       "value": 1 
      }, 
      { 
       "key": "Part", 
       "value": "A" 
      } 
      ] 
     }, 
     { 
      "sequence": "3", 
      "QuestionId": "57BB6DDC-A90A-10EE-E224-EC658A825871", 
      "metadata": [ 
      { 
       "key": "Group", 
       "value": 1 
      }, 
      { 
       "key": "Part", 
       "value": "B" 
      } 
      ] 
     } 
     ] 
    } 
    ] 
} 

質問、私はキーと値のペアを保持するメタデータを持っています。

上記の例では、3つの質問があり、そのうち2つの質問があり、メタデータのキー値は「グループ1」です。

ここでは、質問を同じキーと値のペアと組み合わせて、1つとして扱いたいと考えています。

最終的には、3つではなく2つの質問があります。そのうちの1つの質問では、2つの別個の質問が内部にあります。

そして、私はLinqクエリを使用してこれを実現したいと思います。可能であれば、必要に応じてNewtonsoftを解析に使用してください。私はこれに長い間立ち往生してきました。私が行っている

もの:

public virtual HttpResponseMessage AddQuestionsToStandardMaster(TaxonomyMetaData objQuestion) 
    { 
     List<ResponseEntity> objResponseList = new List<ResponseEntity>(); 
     try 
     { 
      if (ModelState.IsValid) 
      { 
       foreach (var objEntity in objQuestion.Entities) 
       { 
        EntityResponse objentityresponse = new EntityResponse(); 
        ResponseEntity objResponse = new ResponseEntity(); 
       } 
       List<Question> objQuestionList = new List<Question>(); 
          if (objEntity.Questions.Length > 0) 
          { 
          foreach (var item in objEntity.Questions) 
           { 
            int questionTypeid = 0; 

            dynamic objQuestionJson = JObject.Parse(item.ToString()) 
          } 
         } 
      } 
      Question objCurrentQuestion = new Question(); 
            Question objQuestionforDelete = new Question(); 
            JObject itemToParese = new JObject(); 
            string SingleQuestionJson = objQuestionJson.GetValue("QuestionData").ToString(); 
            string questionstem = ""; 
            Regex rgx = new Regex("/\'"); 
            objCurrentQuestion.Sequence = Convert.ToInt32(objQuestionJson.GetValue("sequence")); 
            objCurrentQuestion.tag = objQuestionJson.tag.ToObject<JToken[]>(); ; 
            objCurrentQuestion.metadata = objQuestionJson.metadata.ToObject<JToken[]>(); 
            objCurrentQuestion.SingleQuestionJson = rgx.Replace(SingleQuestionJson, "'"); 
            objCurrentQuestion.QuestionsType = questionTypeid; 
            objCurrentQuestion.QuestionsId = new Guid(objQuestionJson.GetValue("QuestionId").ToString()); 
            objCurrentQuestion.VersionNo = Convert.ToInt32(objQuestionJson.GetValue("VersionNo")); 
            objCurrentQuestion.DisplayQuestionId = Convert.ToString(objQuestionJson.GetValue("DisplayQuestionId")); 
            objCurrentQuestion.OriginalQuestionId = Convert.ToString(objQuestionJson.GetValue("OriginalQuestionId")); 
            objCurrentQuestion.PassageText = Convert.ToString(objQuestionJson.GetValue("passage_text")); 
            objCurrentQuestion.PassageCode = Convert.ToString(objQuestionJson.GetValue("passage_id")); 
            objCurrentQuestion.PassageTitle = Convert.ToString(objQuestionJson.GetValue("passage_title")); 
            objCurrentQuestion.IsPublished = Convert.ToByte(true); 
            objCurrentQuestion.ProductId = objEntity.ProductID; 

            foreach (var metadata in objCurrentQuestion.metadata) 
            { 
             switch (metadata["key"].ToString()) 
             { 
              case "Group": 
               objCurrentQuestion.Group = Convert.ToInt32(metadata["value"].ToString()); 
               break; 

              case "Part": 
               objCurrentQuestion.Part = metadata["value"].ToString(); 
               break; 
             } 

            } 
            objQuestionList.Add(objCurrentQuestion); 
            int counter = 1; 
           //Here I get the data in a group which needs to coverted to JSOn and then replace the original JSON data with this. But I know this is irrelevant to what we need to achieve. 
           var yui = objQuestionList.Where(tma => tma.Group == counter).Select(t => t).GroupBy(s => new { s.Group }).Where(p => p.Count() > 1).ToList(); 

           //After proper conversion I need to enter this data to a database. 
+0

これまでに何を試しましたか? –

+0

あなたはこれから抜け出したいC#オブジェクトについて説明してもらえますか?私はあなたがデータをどのようにグループ化したいかについて少しはっきりしていませんか? –

+0

変更後、上記と同じJSON形式にします。 –

答えて

0

私はあなたがすることによって意味するか何に少しは不明だそれ

は同じキーと値のペアで質問を組み合わせて1として処理します

ここでは、JSONによってメタデータ内のグループにグループ化して、どのようにしてカスタム選択を行うことができるかを示します。 「グループ」は常に配列の拳項目でない場合

var text = @"{ 
""entities"":[ 
    { 
     ""republish"": false, 
     ""OrgID"": """", 
     ""createdby"": ""730"", 
     ""questions"": [ 
     { 
      ""sequence"": ""5"", 
      ""QuestionId"": ""57BB6DDC-A90A-10EE-E224-EC658A825871"", 
      ""metadata"": [ 
      { 
       ""key"": ""Group"", 
       ""value"": 0 
      }, 
      { 
       ""key"": ""Part"", 
       ""value"": ""0"" 
      } 
      ] 
     }, 
     { 
      ""sequence"": ""4"", 
      ""QuestionId"": ""57BB6DDC-A90A-10EE-E224-EC658A825871"", 
      ""metadata"": [ 
      { 
       ""key"": ""Group"", 
       ""value"": 1 
      }, 
      { 
       ""key"": ""Part"", 
       ""value"": ""A"" 
      } 
      ] 
     }, 
     { 
      ""sequence"": ""3"", 
      ""QuestionId"": ""57BB6DDC-A90A-10EE-E224-EC658A825871"", 
      ""metadata"": [ 
      { 
       ""key"": ""Group"", 
       ""value"": 1 
      }, 
      { 
       ""key"": ""Part"", 
       ""value"": ""B"" 
      } 
      ] 
     } 
     ] 
    } 
    ] 
}"; 
      var json = JObject.Parse(text); 

      var groupedData = from entity in json["entities"] 
           from question in entity["questions"] 
           group question by question["metadata"][0]["value"] into questionGroup 
           select questionGroup; 

      foreach (var data in groupedData) 
      { 
       Console.WriteLine("_____________________"); 
       Console.WriteLine("Group"); 
       Console.WriteLine(data.Key); 

       foreach (var question in data) 
       { 
        Console.WriteLine(question["QuestionId"]); 
       } 
       Console.WriteLine("_____________________"); 
      } 

あなたは

question["metadata"].First(md => md.Value<string>("key") == "Group")["value"] 

はそれを得るために行うことができます。

関連する問題