2016-12-20 4 views
0

Ich habe ein paar Tische zusammengeschlossen haben, die einen Datensatz erzeugt, der wie folgt aussieht:Liste der Daten-Un abflachen effizient

GroupName: "Products and Services"; 
GroupSortOrder: 0 
TopicName: "Money Transfer" 
TopicOrder: 11 
Question: "Money Transfer question 1" 
Answer: "Money Transfer answer 1" 
ItemOrder: 0 

[für jedoch viele Elemente in einer Liste wiederholt]

Meine Absicht ist, mit einem JSON Ergebnis am Ende, die wie folgt aussieht:

var jsonResult = [{ 
    title: 'Products and Services', 
    order: 0, 
    items: [{ 
     text: 'Money Transfer', 
     order: 11, 
     questionsAndAnswers: [{ 
      question: 'Money transfer question 1', 
      answer: 'Money transfer answer 1', 
      order: 0 
     }] 
    }] 
}]; 

ich ein paar Dinge, einschließlich LINQ GroupBy versucht haben, die unfo Es platziert den gruppierten Wert in den Schlüssel und kann auch nicht die zugehörigen Werte (Reihenfolge, Elemente) zusammen mit dem "Gruppennamen" verschieben.

Unten ist mein nächster Versuch, der mit den richtigen Gruppen und "Gruppenniveaudaten" endet, aber jede Gruppe enthält alle Themen und jedes Thema hat alle Fragen, nicht nur die, die damit zusammenhängen Gruppe und Thema "Kategorie".

AKTUALISIERT NÄHER SOLUTION

 /// <summary> 
     /// First organizes all FAQ items by topic property, removes topic data by from item level and groups results by topic desc 
     /// </summary> 
     /// <param name="allFaqItemsInSelectedSystem">2d collection of all faq items to be restructured.</param> 
     /// <param name="outErrors">Errors out.</param> 
     /// <returns>JSON string containing questionAnswer data inside of related topic objects</returns> 
     public string SortAndRestructureFaqItemsData(List<FAQQuestionAnswer> allFaqItemsInSelectedSystem, out string outErrors) 
     { 
      string jsonResult = string.Empty; 
      string errors = string.Empty; 

      // Final result, collection of unique groups 
      List<FAQGroupedTopicsItemsQuestionsAnswers> groupsCollection = new List<FAQGroupedTopicsItemsQuestionsAnswers>(); 

      // Collection for FAQQuestionAnswerOnly. 
      List<FAQQuestionAnswerOnly> faqItemsForTopic = new List<FAQQuestionAnswerOnly>(); 

      foreach (var faq in allFaqItemsInSelectedSystem) 
      { 
       // Get topic name from faq data. 
       string topicName = faq.TopicName; 

       // Get group name from faq data. 
       string groupName = faq.GroupName; 

       // New temp topic. 
       FAQTopicsWithItemsResult newTopic = new FAQTopicsWithItemsResult(topicName, faq.TopicOrder, faqItemsForTopic); 

       // Check if this group exists. 
       if (groupsCollection.Any(faqGroup => faqGroup.GroupName == faq.GroupName)) 
       { 
        // Check if the topic exists inside of this group 
        if (groupsCollection.Any(faqGroup => faqGroup.GroupTopics.Any(faqTopic => faqTopic.Title == topicName))) 
        { 
         // Since the and group exists, add the newTopic to it which also contains the first item. 
         groupsCollection.Find(faqGroup => faqGroup.GroupName == groupName).GroupTopics.Find(faqTopic => faqTopic.Title == topicName).Items.Add(new FAQQuestionAnswerOnly(faq.Question, faq.Answer, faq.ItemOrder)); 
        } 
        else 
        { 
         // Since the group exists but not the topic, add the new topic containing the new item to the matching group. 
         groupsCollection.Find(faqGroup => faqGroup.GroupName == groupName).GroupTopics.Add(newTopic); 
        } 
       } 
       else 
       { 
        // Since this is a new group, add it along with the new topic, and faqItem 
        groupsCollection.Add(new FAQGroupedTopicsItemsQuestionsAnswers(groupName, faq.GroupSortOrder, new List<FAQTopicsWithItemsResult> { newTopic })); 
       } 
      } 

      try 
      { 
       jsonResult = JsonConvert.SerializeObject(groupsCollection); 
      } 
      catch (Exception error) 
      { 
       errors = error.Message; 
      } 

      outErrors = errors; 

      return jsonResult; 
     } 

Ich würde das Ergebnis veröffentlichen, aber es ist zu groß. Hier ist eine Kurzfassung:

var jsonResult = [{ 
    title: 'Products and Services', 
    order: 0, 
    items: [{ 
     text: 'Money Transfer', 
     order: 11, 
     questionsAndAnswers: [{ 
      question: 'Money transfer question 1', 
      answer: 'Money transfer answer 1', 
      order: 0 
     }, 
     { ... plus all items in all topics instead of just the ones in the 'Money Transfer' topic. } 
     ] 
    }, 
     { ... plus all topics in all groups, instead of just the ones in the 'Products and services' topic. }] 
}]; 

Vielen Dank im Voraus für jede Hilfe, die jemand bieten kann. Ich bin offen für eine einfachere Lösung, wenn es eine gibt.

Antwort

1

endete ich mit diesem auf meinem eigenen up, das ist genau das, was ich brauchte :)

/// <summary> 
    /// First organizes all FAQ items by topic property, removes topic data by from item level and groups results by topic desc 
    /// </summary> 
    /// <param name="allFaqItemsInSelectedSystem">2d collection of all faq items to be restructured.</param> 
    /// <param name="outErrors">Errors out.</param> 
    /// <returns>JSON string containing questionAnswer data inside of related topic objects</returns> 
    public string SortAndRestructureFaqItemsData(List<FAQQuestionAnswer> allFaqItemsInSelectedSystem, out string outErrors) 
    { 
     string jsonResult = string.Empty; 
     string errors = string.Empty; 

     // Final result, collection of unique groups 
     List<FAQGroupedTopicsItemsQuestionsAnswers> groupsCollection = new List<FAQGroupedTopicsItemsQuestionsAnswers>(); 

     foreach (var faq in allFaqItemsInSelectedSystem) 
     { 
      // Get topic name from faq data. 
      string topicName = faq.TopicName; 

      // Get group name from faq data. 
      string groupName = faq.GroupName; 

      // Is this a new group or existing? 
      bool groupExists = groupsCollection.Any(faqGroup => faqGroup.GroupName == faq.GroupName); 

      // Is this topic new or existing? 
      bool topicExists = groupsCollection.Any(faqGroup => faqGroup.GroupTopics.Any(faqTopic => faqTopic.Title == topicName)); 

      // New temp topic. 
      FAQTopicsWithItemsResult newTopic = new FAQTopicsWithItemsResult(topicName, faq.TopicOrder, new List<FAQQuestionAnswerOnly>()); 

      // Check if this group exists. 
      if (groupExists) 
      { 
       // Check if the topic exists inside of this group 
       if (topicExists) 
       { 
        // Since the and group exists, add the newTopic to it which also contains the first item. 
        groupsCollection.Find(faqGroup => faqGroup.GroupName == groupName).GroupTopics.Find(faqTopic => faqTopic.Title == topicName).Items.Add(new FAQQuestionAnswerOnly(faq.Question, faq.Answer, faq.ItemOrder)); 
       } 
       else 
       { 
        // Since the group exists but not the topic, add the new topic containing the new item to the matching group. 
        groupsCollection.Find(faqGroup => faqGroup.GroupName == groupName).GroupTopics.Add(newTopic); 
       } 
      } 
      else 
      { 
       // Since this is a new group, add it along with the new topic, and new faqItem 
       groupsCollection.Add(new FAQGroupedTopicsItemsQuestionsAnswers(groupName, faq.GroupSortOrder, new List<FAQTopicsWithItemsResult> { newTopic })); 
      } 
     } 

     try 
     { 
      jsonResult = JsonConvert.SerializeObject(groupsCollection); 
     } 
     catch (Exception error) 
     { 
      errors = error.Message; 
     } 

     outErrors = errors; 

     return jsonResult; 
    } 
Verwandte Themen