如何从dictonary到datatable检索值 [英] How to retrieve values from dictonary to datatable
问题描述
嗨
我有从遍历记事本文件收集的数据列表并将其存储在字典中,最后我打印输出在控制台。现在我需要将这些数据放入数据表中然后打印出来。有谁能建议我怎么做或实现?
我的代码
Hi
I have list of data collected from traversing the notepad file and storing it in a dictionary and finally i print the output in console. Now i need to take this data into the datatable and then print it. Can anyone suggest me how to do this or achieve?
My code
static void unzip()
{
try
{
int counter = 1;
var files = new List<string>(Directory.GetFiles(path, "*.zip*", SearchOption.AllDirectories));
Dictionary<string, int> Messagetype = new Dictionary<string, int>();
foreach (var item in files)
{
try
{
// Dictionary<string, int> Messagetype = new Dictionary<string, int>();
//var zipFileName = item;
//var targetDir = Path.Combine(path, "unpack");
//FastZip fastZip = new FastZip();
//string fileFilter = null;
//fastZip.ExtractZip(item, targetDir, fileFilter);
Console.WriteLine(counter++);
Console.WriteLine("Unzipping " + counter + item.Substring(52));
using (var zipInputStream = new ZipInputStream(File.OpenRead(item)))
{
using (var fileStream = new FileStream(item, FileMode.Open, FileAccess.Read))
{
var zipFile = new ZipFile(fileStream);
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.GetNextEntry()) != null)
{
var textStream = zipFile.GetInputStream(zipEntry);
using (var textLine = new StreamReader(textStream))
{
string message;
while ((message = textLine.ReadLine()) != null)
{
if (string.IsNullOrEmpty(message)) continue;
string[] parts = message.Split(new[] { "|" }, StringSplitOptions.None);
if (!Messagetype.ContainsKey(parts[1]))
Messagetype[parts[1]] = 0;
Messagetype[parts[1]]++;
}
}
}
}
}
}
catch (Exception ex1)
{
Console.WriteLine( "File Name - " + item + " Error -" + ex1.Message);
//throw;
}
}
foreach (var ms in Messagetype.Keys)
{
Console.WriteLine(ms + "-" + Messagetype[ms]);
}
我尝试过:
无法将其添加到数据表
What I have tried:
Unable to add it to the datatable
推荐答案
没有内置方法来实现,但你可以编写自定义的帮助类 :
There's no inbuilt method to achieve that, but you can write custom "helper class":
void Main()
{
Dictionary<string, int> Messagetype = new Dictionary<string, int>();
Messagetype.Add("a", 1);
Messagetype.Add("b", 1);
Messagetype.Add("c", 2);
Messagetype.Add("d", 2);
Messagetype.Add("e", 3);
DataTable dt = HelperClass.ToDataTable(Messagetype);
//dt contains Dictionary data
}
// Define other methods and classes here
public static class HelperClass
{
public static DataTable ToDataTable(Dictionary<string, int> d)
{
DataTable dt = new DataTable();
dt.Columns.Add("Item1", typeof(string));
dt.Columns.Add("Item2", typeof(int));
var result = d.Select(x=> dt.LoadDataRow(new object[] {x.Key, x.Value}, false));
dt = result.CopyToDataTable();
return dt;
}
}
欲了解更多详情,请参阅:
DataTable.LoadDataRow Method(Object [],Boolean)(System.Data ) [ ^ ]
创建DataTable从查询(LINQ到DataSet) [ ^ ]
布鲁诺,谢谢你的宝贵意见! br />
[/ EDIT]
For further details, please see:
DataTable.LoadDataRow Method (Object[], Boolean) (System.Data)[^]
Creating a DataTable From a Query (LINQ to DataSet)[^]
Bruno, Thank you for your valuable comment!
[/EDIT]
这篇关于如何从dictonary到datatable检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!