如何将该对象很好地转换为特定格式?

时间:2019-01-20 07:09:59

标签: c# json linq

我有一个List<List<JsonCategoryInfo>>,按user_id分组。

public class JSONCategoryInfo
{
    public int user_id {get; set;}
    public string name {get; set;}
    public int category_id {get; set;}
    public string category {get; set;}
    public int info_id {get; set;}
    public string info_key{get; set;}
    public string info_value{get; set;}
}

我正在尝试将其转换为List<List<JSONFullChildInfo>>

public class JSONFullChildInfo
{
    public JSONFullChildInfo()
    {
        categoriesAndInfo = new List<CategoriesAndInfo>();
    }
    public int user_id {get; set;}
    public string name {get; set;}
    public IList<CategoriesAndInfo> categoriesAndInfo {get; set;} 
}

public class CategoriesAndInfo
{
    public CategoriesAndInfo()
    {
        info = new List<JSONChildInfo>();
    }
    public string category {get; set;}
    public int category_id {get; set;}
    public IList<JSONChildInfo> info {get; set;}
}

public class JSONChildInfo
{
    public int info_id {get; set;}
    public string info_key{get; set;}
    public string info_value{get; set;}
}

我能够做到这一点的唯一方法是使用许多嵌套循环和许多可能不必要的代码行。想知道是否有一种有效的方法吗?

2 个答案:

答案 0 :(得分:0)

我希望我对您的问题的理解是正确的。希望以下对您有所帮助。

var result = listOfList.SelectMany(x=>x).GroupBy(x=>new {x.user_id,x.name}).Select(jfci=>new JSONFullChildInfo
{
    user_id = jfci.Key.user_id,
    name = jfci.Key.name,
    categoriesAndInfo = jfci.ToList().Select(ci=>new CategoriesAndInfo
    {
        category_id= ci.category_id,
        category = ci.category,
        info = jfci.ToList().Select(jci=>new JSONChildInfo
        {
            info_id = jci.info_id,
            info_key = jci.info_key,
            info_value = jci.info_value
        }).ToList()
    }).ToList(),
});;

示例输入

enter image description here

示例输出

enter image description here

答案 1 :(得分:0)

在@Anu的指导下达成了答案

        /// <summary>
        /// Formats triple join into IEnumerable<JSONFullChildInfo>
        /// </summary>
        /// <param name="allCategoryInfo"> List of all categoies/info of particular user(s)</param>
        /// <returns>Formatted json ready output</returns>
        private IEnumerable<JSONFullChildInfo> formatReadOutput(IEnumerable<JSONCategoryInfo> allCategoryInfo)
        {
            return allCategoryInfo
            .GroupBy(cci => new {
                cci.user_id, 
                cci.name
            }) //Groups by user
            .Select(jfci=>new JSONFullChildInfo //Begin formatting
            {
                user_id = jfci.Key.user_id,
                name = jfci.Key.name,
                categoriesAndInfo = jfci
                .Select(ci=>new CategoriesAndInfo
                {
                    category_id= ci.category_id,
                    category = ci.category,
                    info = jfci
                    .Where(ck => ci.category_id == ck.category_id). //Only grab by correct category id
                    Select(jci=>new JSONChildInfo
                    {
                        info_id = jci.info_id,
                        info_key = jci.info_key,
                        info_value = jci.info_value
                    })
                })
                .GroupBy(r => r.category_id) // This and next line gets rid of duplicates
                .Select(g => g.First())
            });
        }