【问题标题】:How can I transform this object into a specific format nicely?我怎样才能很好地将这个对象转换成特定的格式?
【发布时间】:2019-01-20 07:09:58
【问题描述】:

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

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;}
}

我能够做到这一点的唯一方法是使用大量嵌套循环和大量可能不必要的编码行。想知道是否有有效的方法来做到这一点?

【问题讨论】:

  • 欢迎来到stackoverflow。请花一分钟时间拨打tour,尤其是How to Ask。就像现在一样,你的问题太宽泛了,不适合在这里。此外,我们需要看看您已经尝试过什么。

标签: c# json linq


【解决方案1】:

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

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(),
});;

输入示例

示例输出

【讨论】:

  • 非常感谢,我不得不稍微调整一下才能完全回答我的问题,但指导真的很有帮助!
  • @anu 你是怎么得到这个图形视图的?
  • @Sumitraj 那来自 LinqPad
【解决方案2】:

在@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())
            });
        }

【讨论】:

    猜你喜欢
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2023-02-13
    相关资源
    最近更新 更多