Click here to Skip to main content
15,921,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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
Posted
Updated 8-Aug-16 20:41pm

1 solution

There's no inbuilt method to achieve that, but you can write custom "helper class":

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


For further details, please see:
DataTable.LoadDataRow Method (Object[], Boolean) (System.Data)[^]
Creating a DataTable From a Query (LINQ to DataSet)[^]

[EDIT]
Bruno, Thank you for your valuable comment!
[/EDIT]
 
Share this answer
 
v3
Comments
[no name] 9-Aug-16 2:56am    
A 5 but:
a.) DataTable dt = new DataTable();
dt = HelperClass.ToDataTable(Messagetype);
...should be more ...
DataTable dt = HelperClass.ToDataTable(Messagetype);
b.) What happens in case you call ToDataTable(...) more than one time?
Maciej Los 9-Aug-16 3:06am    
Thank you, Bruno.
As to the b): Good catch! Check updated solution ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900