Convert List of Data to csv file - c#

I have 5 datasets coming from Database and have saved them in List like below:
var list1 = new List<Class1>
var list2 = new List<Class2>
var list3 = new List<Class3>
var list4 = new List<Class4>
var list5 = new List<Class5>
I need to convert all 5 lists into a csv file (excel type).
I can do this only for lis1 dataset as of now.
How can we merge all list and print the data in CSV?

The format of the csv file is as follows
Year,Make,Model,Length
1997,Ford,E350,2.35
2000,Mercury,Cougar,2.38
The following code shows how to implement it or you can use ready-made libraries.
public void WriteToCSV()
{
var csv = new StringBuilder();
foreach (var item in list1)
{
string line = "field1,field2,...";
csv.AppendLine(line);
line = string.Format("{0},{1},...",item.field1,...);
csv.AppendLine(line);
}
//.........................................
//.........................................
foreach (var item in list5)
{
string line = "field1,field2,...";
csv.AppendLine(line);
line = string.Format("{0},{1},...",item.field1,...);
csv.AppendLine(line);
}
string fileName = #"D:\WriteText.csv";
if (File.Exists(fileName))
System.IO.File.AppendAllText(fileName, csv.ToString());
else
System.IO.File.WriteAllText(fileName, csv.ToString());
}

Related

Treeview with csv dataset

I need an overview of two floors as a treeview. I have a csv, but don't know how to get it into the treeview. I saw many examples but nothing works. Thanks.
That's how my csv looks like:
floor;room;place;pcnumb
4;1;1;6001
4;1;2;6002
4;1;3;6003
4;1;4;6004
...
5;8;82;5082
5;8;83;5027
5;8;84;5084
5;9;85;5028
var reader = new StreamReader(File.OpenRead(#"csvh.csv"));
List<string> listA = new List<string>();
List<string> listB = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';');
listA.Add(values[0]);
listB.Add(values[1]);
foreach (var column1 in listA)
{
Console.WriteLine(column1);
}
foreach (var column2 in listA)
{
Console.WriteLine(column2);
}
}
That's what I tried, but it doesn't work with my csv

Serialize a csv to json with header record using json.net newtonsoft

I have a simple csv that I am trying to serialize to json. How do I include the header record as the name in the name value pairs?
JsonSerializer serializer = new JsonSerializer();
var csv = new List<string[]>();
var lines = System.IO.File.ReadAllLines(file);
foreach (string line in lines)
csv.Add(line.Split(','));
string json = JsonConvert.SerializeObject(csv, Formatting.Indented);
You can use a List of Dictionary<string,string> but you also need to get the header row from the csv. I have a rough idea for you below.
// initialize the list
var list = new List<Dictionary<string, string>>();
var lines = System.IO.File.ReadAllLines("");
// get your header values
var headers = lines[0].Split(',');
// Here you want to skip the first line because it is the header
foreach (string line in lines.Skip(1))
{
// split your line to get individual values
var lineSplit = line.Split(',');
// make a dictionary to hold the line values
var dictionary = new Dictionary<string, string>();
// do a for loop to apply your headers
for (int i = 0; i < headers.Length; i++)
{
dictionary.Add(headers[i], lineSplit[i]);
}
// Add your dictionary to the list
list.Add(dictionary);
}
string json = JsonConvert.SerializeObject(list, Formatting.Indented);
The above will give you an array that is not wrapped in an object. If you would like something that will wrap it into an object you can just make a simple class to take care of that. Example below.
public class CsvToJson
{
public CsvToJson()
{
this.List = new List<Dictionary<string, string>>();
}
public CsvToJson(string filePath)
{
this.List = new List<Dictionary<string, string>>();
// Adding some exception prevention
if (File.Exists(filePath))
{
ConvertFromCsvToJson(filePath);
}
}
public List<Dictionary<string, string>> List { get; set; }
private void ConvertFromCsvToJson(string filePath)
{
var lines = File.ReadAllLines(filePath);
// get your header values
var headers = lines[0].Split(',');
foreach (string line in lines.Skip(1))
{
// split your line to get individual values
var lineSplit = line.Split(',');
// make a dictionary to hold the line values
var dictionary = new Dictionary<string, string>();
// do a for loop to apply your headers
for (int i = 0; i < headers.Length; i++)
{
dictionary.Add(headers[i], lineSplit[i]);
}
// Add your dictionary to the list
List.Add(dictionary);
}
}
}
Then you could easily call on this anytime by using something like below.
var rootObject = new CsvToJson("C:\testFile.csv");
var json = JsonConvert.SerializeObject(rootObject, Formatting.Indented);

How can I extract a string out of an object that contains this string?

I created a Yaml that looks like this:
Directories:
- ./Libraries:
- DLLList.yml
- ./Output:
- None
Now I deserialized that yaml into a list of Objects:
List<object> allDirectoriesList = new List<object>();
List<string> allFileNames = new List<string>();
using (var reader = new StringReader(File.ReadAllText("./FileConfig.yml")))
{
allDirectoriesList = deserializer.Deserialize<dynamic>(reader)["Directories"] as List<Object>;
}
foreach (var directory in allDirectoriesList)
{
var directoryAsDictionary = (Dictionary<object, object>)directory;
List<object> list = directoryAsDictionary.Select(kvp => kvp.Value).ToList();
IEnumerable<string> _fileList = list.Select(i => i.ToString());
List<string> fileList = _fileList.ToList<string>();
for (int i = 0; i < fileList.Count(); i++)
{
var x = (string)list[i];
}
}
directory is an object of type Dictionary where I converted it into a List in this part:
var directoryAsDictionary = (Dictionary<object, object>)directory;
List<object> list = directoryAsDictionary.Select(kvp => kvp.Value).ToList();
This list contains 1 object of type string, where the filename is stored. But I can't get these strings out of the objects. If I cast them, or convert them ToString(), I always get "System.Collections.Generic.List`1[System.Object]", but it has to be "DLLList.yml" in this case
Assuming you are using YamlDotNet:
List<object> allDirectoriesList = new List<object>();
using (var reader = new StringReader(File.ReadAllText("./FileConfig.yml")))
{
allDirectoriesList = new DeserializerBuilder().Build().Deserialize<dynamic>(reader)["Directories"] as List<object>;
}
foreach (var directory in allDirectoriesList)
{
var directoryAsDictionary = (Dictionary<object, object>)directory;
List<object> list = directoryAsDictionary.SelectMany(kvp => (List<object>)kvp.Value).ToList();
List<string> _fileList = list.Select(Convert.ToString).ToList();
foreach(var file in _fileList)
Console.WriteLine($"Item: {file} found in {Convert.ToString(directoryAsDictionary.Keys.First())}");
}
Basically you were trying to turn the dictionary value to a string, but it was a List. By using SelectMany, it can flatten all the lists into one and use that. There were a few redundant casts, which I've also removed. For future reference, try to make your structures as simple as possible and deserialise them into structs/classes - you'll find this a lot easier that way.

C# Read text file and print out in to a table

I'm trying to read a text file and print out into a table.
I want the output to be this
But now I having different output
var column1 = new List<string>();
var column2 = new List<string>();
var column3 = new List<string>();
using (var rd = new StreamReader(#"C:\test.txt"))
{
while (!rd.EndOfStream)
{
var splits = rd.ReadLine().Split(';');
column1.Add(splits[0]);
column2.Add(splits[1]);
column3.Add(splits[2]);
}
}
Console.WriteLine("Date/Time \t Movie \t Seat");
foreach (var element in column1) Console.WriteLine(element);
foreach (var element in column2) Console.WriteLine(element);
foreach (var element in column3) Console.WriteLine(element);
You can use Linq to construct a convenient structure (e.g. List<String[]>) and then print out all the data wanted:
List<String[]> data = File
.ReadLines(#"C:\test.txt")
//.Skip(1) // <- uncomment this to skip caption if the csv has it
.Select(line => line.Split(';').Take(3).ToArray()) // 3 items only
.ToList();
// Table output (wanted one):
String report = String.Join(Environment.NewLine,
data.Select(items => String.Join("\t", items)));
Console.WriteLine(report);
// Column after column output (actual one)
Console.WriteLine(String.Join(Environment.NewLine, data.Select(item => item[0])));
Console.WriteLine(String.Join(Environment.NewLine, data.Select(item => item[1])));
Console.WriteLine(String.Join(Environment.NewLine, data.Select(item => item[2])));
EDIT: if you want to choose the movie, buy the ticket etc. elaborate the structure:
// Create a custom class where implement your logic
public class MovieRecord {
private Date m_Start;
private String m_Name;
private int m_Seats;
...
public MovieRecord(DateTime start, String name, int seats) {
...
m_Seats = seats;
...
}
...
public String ToString() {
return String.Join("\t", m_Start, m_Name, m_Seats);
}
public void Buy() {...}
...
}
And then convert to conventinal structure:
List<MovieRecord> data = File
.ReadLines(#"C:\test.txt")
//.Skip(1) // <- uncomment this to skip caption if the csv has it
.Select(line => {
String items[] = line.Split(';');
return new MovieRecord(
DateTime.ParseExact(items[0], "PutActualFormat", CultureInfo.InvariantCulture),
items[1],
int.Parse(items[2]));
}
.ToList();
And the table output will be
Console.Write(String.Join(Envrironment.NewLine, data));
Don't use Console.WriteLine if you want to add a "column". You should also use a single List<string[]> instead of multiple List<string>.
List<string[]> allLineFields = new List<string[]>();
using (var rd = new StreamReader(#"C:\test.txt"))
{
while (!rd.EndOfStream)
{
var splits = rd.ReadLine().Split(';');
allLineFields.Add(splits);
}
}
Console.WriteLine("Date/Time \t Movie \t Seat");
foreach(string[] line in allLineFields)
Console.WriteLine(String.Join("\t", line));
In general you should use a real csv parser if you want to parse a csv-file, not string methods or regex.
You could use the TextFieldParser which is the only one available in the framework directly:
var allLineFields = new List<string[]>();
using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(#"C:\test.txt"))
{
parser.Delimiters = new string[] { ";" };
parser.HasFieldsEnclosedInQuotes = false; // very useful
string[] lineFields;
while ((lineFields = parser.ReadFields()) != null)
{
allLineFields.Add(lineFields);
}
}
You need to add a reference to the Microsoft.VisualBasic dll to your project.
There are other available: Parsing CSV files in C#, with header
You could attempt to solve this in a more Object-Orientated manner, which might make it a bit easier for you to work with:
You can declare a simple class to represent a movie seat:
class MovieSeat
{
public readonly string Date, Name, Number;
public MovieSeat(string source)
{
string[] data = source.Split(';');
Date = data[0];
Name = data[1];
Number = data[2];
}
}
And then you can read in and print out the data in a few lines of code:
// Read in the text file and create a new MovieSeat object for each line in the file.
// Iterate over all MovieSeat objets and print them to console.
foreach(var seat in File.ReadAllLines(#"C:\test.txt").Select(x => new MovieSeat(x)))
Console.WriteLine(string.Join("\t", seat.Date, seat.Name, seat.Number));

change string into var datatype in c#

I am reading data from file like this
using (StreamReader r = new StreamReader("newfile.txt"))
{
string lines1;
lines1 = r.ReadLine();
var lines = lines1;
foreach (string line in lines)
{
var dictionary = new Dictionary<String, Int32>();
var records = line.Split(',');
How can i convert string data type to var data type?
if i used this line
var lines = lines1;
then the error accur at
var records = line.Split(',');
There is no such thing as a var datatype. "var", in C#, just means "let the compiler figure out the data type for me based on the context". in this case, using:
var lines = lines1;
or:
string lines = lines1;
Will produce the exact same code. The error message is not related to the usage of var instead of string.
The problem here is this:
lines1 = r.ReadLine();
var lines = lines1;
// ...
foreach (string line in lines)
You're reading a single line as a single string, then trying to do: "foreach string in my single string". I suspect you want something more like:
using (StreamReader r = new StreamReader("newfile.txt"))
{
while (r.Peek() >= 0)
{
string line = r.ReadLine();
var dictionary = new Dictionary<String, Int32>();
var records = line.Split(',');
// use records.... Note that above is the same as:
// string[] records = line.Split(',');
}
}
The "split" function returns an array.

Categories