You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C#扩展方法将扁平列表按Code分组转换为层级数据

How to Convert Flat Data to Hierarchical Structure with C# Extension Methods

Got it, let's tackle this problem of transforming your flat ItemAssignmentFlatList data into the hierarchical ItemInfo structure (with nested TaxPlan lists) using clean, optimized C# extension methods. First, let's recap the requirements and fix up the existing code.

First, Let's Confirm the Class Definitions

Just to make sure we're on the same page, here are the source and target classes you provided:

Source flat data class:

public class ItemAssignmentFlatList 
{ 
    public int Code { get; set; } 
    public string Type { get; set; } 
    public string Description { get; set; } 
    public int ItemCode{ get; set; } 
    public DateTime EffectiveDate{ get; set; } 
    public string Area{ get; set; } 
    public string TaxCode{ get; set; } 
    public string LocationId { get; set; } 
}

Target hierarchical classes:

public class ItemInfo 
{ 
    public int Code { get; set; } 
    public string Type { get; set; } 
    public string Description { get; set; } 
    public List<TaxPlan> TaxPlan { get; set; } 
}

public class TaxPlan 
{ 
    public int ItemCode{ get; set; } 
    public DateTime EffectiveDate{ get; set; } 
    public string Area{ get; set; } 
    public string TaxCode{ get; set; } 
    public string LocationId { get; set; } 
}

Issues with the Existing Code

Your current code works, but has a few areas for improvement:

  • Unnecessary calls to FirstOrDefault(): Since you're grouping by Code and Type, every item in the group shares those values—you can use the group's key directly instead.
  • Incomplete property mapping: Your TaxPlan projection only maps ItemCode and uses an incorrect TaxArea (the source class has Area instead).
  • Lack of reusability: The logic isn't encapsulated, making it hard to reuse across your codebase.

Optimized Extension Method Solution

Let's wrap this logic in a clean extension method that fixes these issues and makes your code more maintainable:

public static class ItemAssignmentExtensions
{
    public static List<ItemInfo> ToHierarchicalItemInfo(this IEnumerable<ItemAssignmentFlatList> flatItems)
    {
        // Guard clause to handle null input (optional but good practice)
        if (flatItems == null)
            throw new ArgumentNullException(nameof(flatItems));

        return flatItems
            // Group by Code + Type + Description (assuming same Code/Type have same Description)
            .GroupBy(item => new { item.Code, item.Type, item.Description })
            // Project each group to an ItemInfo
            .Select(group => new ItemInfo
            {
                Code = group.Key.Code,
                Type = group.Key.Type,
                Description = group.Key.Description,
                // Project all items in the group to TaxPlan objects
                TaxPlan = group.Select(flatItem => new TaxPlan
                {
                    ItemCode = flatItem.ItemCode,
                    EffectiveDate = flatItem.EffectiveDate,
                    Area = flatItem.Area,
                    TaxCode = flatItem.TaxCode,
                    LocationId = flatItem.LocationId
                }).ToList()
            })
            .ToList();
    }
}

Key Improvements Explained

  • Extension Method Encapsulation: Now you can call this method directly on any IEnumerable<ItemAssignmentFlatList>—no need to repeat the grouping logic everywhere.
  • Use Group Key Instead of FirstOrDefault: The group's key contains the shared Code, Type, and Description (assuming your business logic guarantees same Code/Type pairs have the same Description). This is more efficient and avoids unnecessary enumeration.
  • Complete Property Mapping: All TaxPlan properties are correctly mapped from the source data.
  • Null Safety: Added a guard clause to handle null input gracefully.

If Description Might Vary for Same Code/Type

If your data allows for different Description values under the same Code and Type, adjust the grouping to only use Code and Type, then pick the first Description from the group:

public static List<ItemInfo> ToHierarchicalItemInfo(this IEnumerable<ItemAssignmentFlatList> flatItems)
{
    if (flatItems == null)
        throw new ArgumentNullException(nameof(flatItems));

    return flatItems
        .GroupBy(item => new { item.Code, item.Type })
        .Select(group => new ItemInfo
        {
            Code = group.Key.Code,
            Type = group.Key.Type,
            Description = group.First().Description, // Pick first description in the group
            TaxPlan = group.Select(flatItem => new TaxPlan
            {
                ItemCode = flatItem.ItemCode,
                EffectiveDate = flatItem.EffectiveDate,
                Area = flatItem.Area,
                TaxCode = flatItem.TaxCode,
                LocationId = flatItem.LocationId
            }).ToList()
        })
        .ToList();
}

How to Use It

Using the extension method is straightforward:

// Assume 'results' is your IEnumerable<ItemAssignmentFlatList>
var hierarchicalResult = results.ToHierarchicalItemInfo();

内容的提问来源于stack exchange,提问作者VR1256

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:21:42