猿问

c# - 如何使用内置库将字符串的DataTable映射到C#中的整数DataTable?

我有一组 DataTables,每个都有一组行和一组列,所有字符串。我正在寻找最好的方法(即最短的代码长度和可选的最有效的性能)将这些数据表中的每一个映射到具有整数值的数据表,这样相同的字符串将始终由相同的整数值表示跨所有表,也在同一个表中(不编写我自己的循环/等,因为一种选择是循环所有数据并从字符串中创建整数)。


我的问题是:c# 中是否有任何内置库可以通过调用方法来执行此任务?


假设: a) 有数千个表,表中有数百万个条目。b) 解决方案可能区分大小写或不区分大小写。


假设这样的例子:


DataTableInStrings1.Rows[10]["Column10"] = "val1";

DataTableInStrings2.Rows[2]["Column1"] = "val1";

预期的短代码:


DataTable dataTableInIntegers1 = LibIAmAfter.MethodIAmAfter(DataTableInStrings1)

DataTable dataTableInIntegers2 = LibIAmAfter.MethodIAmAfter(DataTableInStrings2)

预期结果


dataTableInIntegers1.Rows[10]["Column10"]=12; 

dataTableInIntegers2.Rows[2]["Column1"]=12;

而整数 12 可以是任何值。这里是随机选择来说明目标的。


==> 另一个帮助理解问题的例子:


row1InStrings: ("abc","def","abc","zxv","was","morning","def","dr","tr","uy");

row2InStrings: ("abc2","def2","abc3","zxv4","was4","morning2","def2","dr3","tr3","uy");

映射后的示例结果:


row1InIntegers: 1,2,1,3,4,5,2,6,7,8

row2InIntegers: 10,11,12,14,15,16,11,17,18,8


SMILET
浏览 234回答 2
2回答

qq_笑_17

一种方法是使用 aDictionary<string, int>将单元格数据存储为键,将映射整数存储为值。请注意,您不必一次用整个数据集填充映射。只要您维护一个映射字典,您就可以在访问项目时简单地填充它。请注意,这只会保证字符串的值是唯一的,但在后续运行中不一定是相同的值(因为这些值基于请求 id 的时间而不是字符串本身)。像这个带有私有字段和访问方法的静态类应该可以工作(尽管不是线程安全的):public static class Mapper{&nbsp; &nbsp; private static readonly Dictionary<string, int> Mapping =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);&nbsp; &nbsp; public static int GetId(string value)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int result;&nbsp; &nbsp; &nbsp; &nbsp; if (!Mapping.TryGetValue(value, out result))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = Mapping.Count + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Mapping.Add(value, result);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }}使用此方法,我们可以根据需要获取映射,并且仅在必要时填充字典:DataTable tbl1 = new DataTable("table1");tbl1.Columns.Add(new DataColumn("col1"));tbl1.Columns.Add(new DataColumn("col2"));tbl1.Columns.Add(new DataColumn("col3"));tbl1.Columns.Add(new DataColumn("col4"));tbl1.Columns.Add(new DataColumn("col5"));tbl1.Columns.Add(new DataColumn("col6"));tbl1.Columns.Add(new DataColumn("col7"));tbl1.Columns.Add(new DataColumn("col8"));tbl1.Columns.Add(new DataColumn("col9"));tbl1.Columns.Add(new DataColumn("col10"));tbl1.Rows.Add("abc", "def", "abc", "zxv", "was", "morning", "def", "dr", "tr", "uy");tbl1.Rows.Add("abc2", "def2", "abc3", "zxv4", "was4", "Morning", "def2", "dr3", "tr3", "uy");// Output mappings, which populates the dictionary&nbsp; &nbsp; &nbsp;// only when needed as each mapping is requestedforeach (DataRow row in tbl1.Rows){&nbsp; &nbsp; Console.WriteLine(string.Join(",",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; row.ItemArray.Select(item => Mapper.GetId(item.ToString()))));}输出

LEATH

您可以使用确定性 guid 来创建唯一的哈希。此外,您可以将值本身用作它自己的唯一哈希。如果由于某种原因,您无法向用户显示原始值但仍想在列表中找到它,我只能看到这很有用。例如,一组密码。&nbsp; &nbsp; [TestMethod]&nbsp; &nbsp; public void test_sum_stringchars()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string tmp = "foobar5";&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Value = " + tmp.ToCharArray().Sum(x => x));&nbsp; &nbsp; &nbsp; &nbsp; // 686&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Value = " + ToGuidKey(tmp));&nbsp; &nbsp; &nbsp; &nbsp; // 79ceeb8d&nbsp; &nbsp; &nbsp; &nbsp; tmp = "foobar6";&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Value = " + tmp.ToCharArray().Sum(x => x));&nbsp; &nbsp; &nbsp; &nbsp; // 687&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Value = " + ToGuidKey(tmp));&nbsp; &nbsp; &nbsp; &nbsp; // f1f08c51&nbsp; &nbsp; &nbsp; &nbsp; tmp = "goobar5";&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Value = " + tmp.ToCharArray().Sum(x => x));&nbsp; &nbsp; &nbsp; &nbsp; // 687&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Value = " + ToGuidKey(tmp));&nbsp; &nbsp; &nbsp; &nbsp; // f7da9f42&nbsp; &nbsp; &nbsp; &nbsp; tmp = "foocar5";&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Value = " + tmp.ToCharArray().Sum(x => x));&nbsp; &nbsp; &nbsp; &nbsp; // 687&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Value = " + ToGuidKey(tmp));&nbsp; &nbsp; &nbsp; &nbsp; // 7698c7ec&nbsp; &nbsp; }&nbsp; &nbsp; public static Guid ToGuid(string src)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; byte[] stringbytes = System.Text.Encoding.UTF8.GetBytes(src);&nbsp; &nbsp; &nbsp; &nbsp; byte[] hashedBytes = new System.Security.Cryptography&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .SHA1CryptoServiceProvider()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ComputeHash(stringbytes);&nbsp; &nbsp; &nbsp; &nbsp; Array.Resize(ref hashedBytes, 16);&nbsp; &nbsp; &nbsp; &nbsp; return new Guid(hashedBytes);&nbsp; &nbsp; }&nbsp; &nbsp; public static string ToGuidKey(string src)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return ToGuid(src).ToString().Split('-').First();&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答