代码之家  ›  专栏  ›  技术社区  ›  Lews Therin

我应该使用什么数据结构将三个“旧”值映射到三个“新”值?

  •  1
  • Lews Therin  · 技术社区  · 5 年前

    我正试图弄清楚什么样的数据结构才有意义来表示我的数据。我正在开发一个C#工具,它将更新一组特定类别、子类别和子子类别的名称。以下是一些示例数据:

    OldCategory | OldSubCategory | OldSubSubCategory | NewCategory           | NewSubCategory | NewSubSubCategory
    -------------------------------------------------------------------------------------------------------------
    Hardware    | Display        | Broken            | HD-Hardware           | Display        |
    Hardware    | Display        | Request upgrade   | Requests              | Hardware       | Display 
    Software    | Excel          | Error message     | HD-Software           | Excel          | General Error
    Software    | Excel          | How Do I          | HD-Software           | Excel          | Training
    Software    | Excel          | Plug-in           | HD-SoftwareExtensions | Excel          |
    

    OldCategory 对应 NewCategory . 数据的构建使得 Category SubCategory ,和 SubSubCategory 是一种独特的价值。例如, Hardware | Display | Request upgrade 将映射到 Requests | Hardware | Display

    我在考虑用 Dictionary<Tuple<string, string, string>, Tuple<string, string, string>> 结构,但对于其他开发人员或我未来的自己来说,这似乎非常冗长,而且可能会让我感到困惑。有什么建议可以让我的数据更清晰地表达出来吗?

    0 回复  |  直到 5 年前
        1
  •  1
  •   Flydog57    5 年前

    以下是我在评论中所说的一个例子:

    public class Category : IEquatable<Category>
    {
        public string MainCategory { get; set; }
        public string SubCategory { get; set; }
        public string SubSubCategory { get; set; }
    
    
        private string GetCharacterizer()
        {
            return $"{MainCategory}#{SubCategory}#{SubSubCategory}";
        }
    
        public override int GetHashCode()
        {
            return GetCharacterizer().GetHashCode();
        }
    
        public bool Equals(Category other)
        {
            if (other == null)
            {
                return false;
            }
            return GetCharacterizer().Equals(other.GetCharacterizer());
        }
    
        public override bool Equals(object other)
        {
            if (!(other is Category catOther))
            {
                return false;
            }
    
            return Equals(catOther);
        }
    }
    

    Dictionary<Category, Category>

        2
  •  1
  •   Vikhram    5 年前

    我建议你走你猜测的同一条路,即。

    Dictionary<Tuple<string, string, string>, Tuple<string, string, string>>
    

    新的 ValueTuple

    var tbl = new Dictionary<(string, string, string), (string, string, string)>();
    var newVal = tbl[("Hardware", "Display", "Request upgrade")];
    

    您还可以使用如下元组字段的名称

    var tbl = new Dictionary<(string Cat, string SubCat, string SubSubCat), (string Cat, string SubCat, string SubSubCat)>();
    

    如果需要更多的灵活性、控制或特性,可以使用如下类

    class CatInfo : Tuple<string, string, string> {
        public string Category => Item1;
        public string SubCategory => Item2;
        public string SubSubCategory => Item3;
    
        public CatInfo(string cat, string subCat, string subSubCat) : base(cat, subCat, subSubCat) { }
    }
    

    如果出于任何原因,您不能从 Tuple ,或者想要稍微更好的性能,那么您只需创建一个如下所示的不可变结构

    struct CatInfo {
        public string Category { get; }
        public string SubCategory { get; } 
        public string SubSubCatergory { get; }
    
        public CatInfo(string cat, string subCat, string subSubCat) {
            Category = cat;
            SubCategory = subCat;
            SubSubCatergory = subSubCat;
        }
    
        public bool Equals(CatInfo other) {
            return string.Equals(Category, other.Category) && string.Equals(SubCategory, other.SubCategory) && string.Equals(SubSubCatergory, other.SubSubCatergory);
        }
    
        public override bool Equals(object obj) {
            if (obj is null)
                return false;
            return obj is CatInfo info && Equals(info);
        }
    
        public override int GetHashCode() {
            unchecked {
                int hashCode = (Category != null ? Category.GetHashCode() : 0);
                hashCode = (hashCode * 397) ^ (SubCategory != null ? SubCategory.GetHashCode() : 0);
                hashCode = (hashCode * 397) ^ (SubSubCatergory != null ? SubSubCatergory.GetHashCode() : 0);
                return hashCode;
            }
        }
    
        public static bool operator ==(CatInfo info1, CatInfo info2) {
            return info1.Equals(info2);
        }
    
        public static bool operator !=(CatInfo info1, CatInfo info2) {
            return !(info1 == info2);
        }
    }
    
        3
  •  0
  •   Bedir    5 年前

    例如。

    public CategoryClass{
        string category;
        string subCategory;
        string subSubCategory;
    }
    

    static void Main(string[] args)
    {
        CategoryClass oldCategory = new CategoryClass();
        CategoryClass newCategory = new CategoryClass();
        Dictionary<CategoryClass, CategoryClass> dict = new Dictionary<CategoryClass, CategoryClass>();
    }
    

    ... 或其他物体。