将Flat数据转换为分层数据

时间:2014-03-19 22:03:36

标签: c# recursion hierarchical-data

我在c#应用程序中获取一些平面数据作为列表。 样本数据google drive spreadsheet

我希望将这些数据转换为分层结构。创建了相同here

的JSON表示

我期待的最终结构是enter image description here

我已将数据创建为C#中的列表。我希望将此列表转换为c#集合对象。请指点我最简单的方法。     [这是一个控制台应用程序代码]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{

    public class Marklist
    {

    public string Student_name { set; get; }
    public string Frequency  { set; get; }
    public string Major  { set; get; }
    public string Subject_category   { set; get; }
    public string Subject_subcategory { set; get; } 
    public int Spring_mark   { set; get; }
    public int Autumn_mark   { set; get; }
    public int Summer_mark { set; get; }



        public Marklist() { }

        public Marklist(string student_name,string frequency,string major,string        subject_category,string subject_subcategory,string spring_mark,string autumn_mark,string summer_mark)
        {

             Student_name =student_name;
             Frequency   =frequency;
              Major  =major;
               Subject_category  =subject_category;
              Subject_subcategory   =subject_subcategory;
            Spring_mark  =spring_mark;
            Autumn_mark  =autumn_mark;
            Summer_mark =summer_mark;

        }

    }

    static void Main(string[] args)
    {

    List<Marklist> Marklists = new List<Marklist>();

        Marklists.Add(new Marklist(){Student_name="Nimalan",Frequency="Monthly",Major="Science",Subject_category="Physics",Subject_subcategory="Atomic",Spring_mark=100,Autumn_mark=95,Summer_mark=75});
        Marklists.Add(new Marklist(){Student_name="Nimalan",Frequency="Monthly",Major="Science", Subject_category="Physics",Subject_subcategory="Nuclear",Spring_mark=95, Autumn_mark=75,Summer_mark=100});
        Marklists.Add(new Marklist(){Student_name="Nimalan",Frequency="Monthly",Major="Science", Subject_category="Physics",Subject_subcategory="Electronics",Spring_mark=75, Autumn_mark=95,Summer_mark=100});
        Marklists.Add(new Marklist(){Student_name="Nimalan",Frequency="Monthly",Major="Language", Subject_category="Tamil",Subject_subcategory="Literature",Spring_mark=80, Autumn_mark=90,Summer_mark=70});
        Marklists.Add(new Marklist(){Student_name="Nimalan",Frequency="Monthly",Major="Commerce" Subject_category="Retail_data",Subject_subcategory="Big_data_analytics",Spring_mark=90, Autumn_mark=80,Summer_mark=70});

        Marklists.Add(new Marklist(){Student_name="Nimalan",Frequency="Weekly",Major="Science",Subject_category="Physics",Subject_subcategory="Atomic",Spring_mark=100,Autumn_mark=95,Summer_mark=75});
        Marklists.Add(new Marklist(){Student_name="Nimalan",Frequency="Weekly",Major="Science", Subject_category="Physics",Subject_subcategory="Nuclear",Spring_mark=95, Autumn_mark=75,Summer_mark=100});
        Marklists.Add(new Marklist(){Student_name="Nimalan",Frequency="Weekly",Major="Science", Subject_category="Physics",Subject_subcategory="Electronics",Spring_mark=75, Autumn_mark=95,Summer_mark=100});
        Marklists.Add(new Marklist(){Student_name="Nimalan",Frequency="Weekly",Major="Language", Subject_category="Tamil",Subject_subcategory="Literature",Spring_mark=80, Autumn_mark=90,Summer_mark=70});
        Marklists.Add(new Marklist(){Student_name="Nimalan",Frequency="Weekly",Major="Commerce" Subject_category="Retail_data",Subject_subcategory="Big_data_analytics",Spring_mark=90, Autumn_mark=80,Summer_mark=70});

        Marklists.Add(new Marklist(){Student_name="StarNimalan",Frequency="Monthly",Major="Science",Subject_category="Physics",Subject_subcategory="Atomic",Spring_mark=100,Autumn_mark=95,Summer_mark=75});
        Marklists.Add(new Marklist(){Student_name="StarNimalan",Frequency="Monthly",Major="Science", Subject_category="Physics",Subject_subcategory="Nuclear",Spring_mark=95, Autumn_mark=75,Summer_mark=100});
        Marklists.Add(new Marklist(){Student_name="StarNimalan",Frequency="Monthly",Major="Science", Subject_category="Physics",Subject_subcategory="Electronics",Spring_mark=75, Autumn_mark=95,Summer_mark=100});
        Marklists.Add(new Marklist(){Student_name="StarNimalan",Frequency="Monthly",Major="Language", Subject_category="Tamil",Subject_subcategory="Literature",Spring_mark=80, Autumn_mark=90,Summer_mark=70});
        Marklists.Add(new Marklist(){Student_name="StarNimalan",Frequency="Monthly",Major="Commerce" Subject_category="Retail_data",Subject_subcategory="Big_data_analytics",Spring_mark=90, Autumn_mark=80,Summer_mark=70});

    }
}
}

2 个答案:

答案 0 :(得分:1)

使用jsonC#http://json2csharp.com/#

从json数据创建的这个类
public class Mark
{
public string markname { get; set; }
public int mark { get; set; }
}

public class SubjectSubcategory
{
public string name { get; set; }
public List<Mark> Marks { get; set; }
}

public class SubjectCategory
{
public string name { get; set; }
public List<SubjectSubcategory> Subject_subcategory { get; set; }
}

 public class Major
{
public string name { get; set; }
public List<SubjectCategory> Subject_category { get; set; }
 }

 public class Frequency
 {
public string name { get; set; }
public List<Major> Major { get; set; }
 }

public class Product
{
public string studentname { get; set; }
 public List<Frequency> frequency { get; set; }
 }

public class RootObject
{
public List<Product> product { get; set; }
}

答案 1 :(得分:0)

第一组按学生姓名,这将为每个学生提供一份包含数据行集合的独特记录。然后对于每一个,按频率分组,然后按主要分组,依此类推。