我已经加入了几个表,这些表生成了一个如下所示的数据集:
GroupName: "Products and Services";
GroupSortOrder: 0
TopicName: "Money Transfer"
TopicOrder: 11
Question: "Money Transfer question 1"
Answer: "Money Transfer answer 1"
ItemOrder: 0
[在许多项目的列表中重复]
我的目的是最终得到一个JSON结果,如下所示:
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
}]
}]
}];
我尝试了很多东西,包括LINQ的GroupBy,遗憾的是,它将分组的值放在键中,也无法将相关值(顺序,项目)与"组名称&#一起移动34。
以下是我最接近的尝试,最后是正确的群组和"群组级数据"但是每个群组都包含所有主题,每个主题都包含所有问题,而不仅仅是与该组和主题相关"类别"。
更新,更接近解决方案
/// <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;
}
我会发布结果,但它太大了。这是一个缩写版本:
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. }]
}];
提前感谢任何人都可以提供的任何帮助。如果有的话我会更容易解决。
答案 0 :(得分:1)
我自己最终得到了这个,这正是我需要的东西:)
/// <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;
}