C
C#ā€¢2y ago
Ash_

ā” shorter way to export a list to a csv?

Hey, i am searching for a quicker way to export a list to a csv with head than my solution under this, could anyone help me shorten this?
public static string AsCsv()
{
string filename = DateTime.Now.ToString(DateTime.Now.ToString("yyyy-MM-dd_HH:mm"))+"-Artikel.csv";
TextWriter tw = new StreamWriter("../../Exports/"+filename);
for (int i = -1; i < Auftrag.Auftragsliste.Count; i++)
{
if (i==-1)
{
_line = "GTIN" + Stringseperator + "Artikelnummer" + Stringseperator + "Bezeichnung"
+ Stringseperator + "Warengruppe" + Stringseperator + "Preis";
}
else
{
for (int j = 0; j < 5; j++)
{
switch (j)
{
case 0:
_gtin = Auftrag.Auftragsliste.ElementAt(i).Gtin;
break;
case 1:
_artikelnummer = Auftrag.Auftragsliste.ElementAt(i).Artikelnummer;
break;
case 2:
_bezeichnung = Auftrag.Auftragsliste.ElementAt(i).Bezeichnung;
break;
case 3:
_warengruppe = Auftrag.Auftragsliste.ElementAt(i).Warengruppe;
break;
case 4:
_preis = Auftrag.Auftragsliste.ElementAt(i).Preis;
break;
}
}
_line = _gtin + Stringseperator + _artikelnummer + Stringseperator + _bezeichnung + Stringseperator + _warengruppe + Stringseperator + _preis;
}
tw.WriteLine(_line);
}
tw.Close();
return filename;
}
public static string AsCsv()
{
string filename = DateTime.Now.ToString(DateTime.Now.ToString("yyyy-MM-dd_HH:mm"))+"-Artikel.csv";
TextWriter tw = new StreamWriter("../../Exports/"+filename);
for (int i = -1; i < Auftrag.Auftragsliste.Count; i++)
{
if (i==-1)
{
_line = "GTIN" + Stringseperator + "Artikelnummer" + Stringseperator + "Bezeichnung"
+ Stringseperator + "Warengruppe" + Stringseperator + "Preis";
}
else
{
for (int j = 0; j < 5; j++)
{
switch (j)
{
case 0:
_gtin = Auftrag.Auftragsliste.ElementAt(i).Gtin;
break;
case 1:
_artikelnummer = Auftrag.Auftragsliste.ElementAt(i).Artikelnummer;
break;
case 2:
_bezeichnung = Auftrag.Auftragsliste.ElementAt(i).Bezeichnung;
break;
case 3:
_warengruppe = Auftrag.Auftragsliste.ElementAt(i).Warengruppe;
break;
case 4:
_preis = Auftrag.Auftragsliste.ElementAt(i).Preis;
break;
}
}
_line = _gtin + Stringseperator + _artikelnummer + Stringseperator + _bezeichnung + Stringseperator + _warengruppe + Stringseperator + _preis;
}
tw.WriteLine(_line);
}
tw.Close();
return filename;
}
53 Replies
Ash_
Ash_OPā€¢2y ago
Discord is pretty bad at keeping lines, if you want to reference a line please use the line numbers from this https://pastebin.com/jEv6FWpk
Pastebin
public static string AsCsv(){ string filename = DateTime.Now.ToS...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
ero
eroā€¢2y ago
What's with the loop starting at -1, and using fields?
Ash_
Ash_OPā€¢2y ago
the loop, personal preference, 0 is the first element for me and the head is no relevant data for me, hence the head is -1 for me, and i'm sorry but i don't know what field means, i'm not that good at english
ero
eroā€¢2y ago
It's not got anything to do with English, fields are just a general concept in C#
Ash_
Ash_OPā€¢2y ago
could you reference the line(s) for this? i code mostly in german and don't know what a field is regarding c#?
Angius
Angiusā€¢2y ago
i code mostly in german
šŸ’€ $structure
MODiX
MODiXā€¢2y ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;
protected double protectedField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;
protected double protectedField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
For C# versions older than 10, see $StructureOld
Ash_
Ash_OPā€¢2y ago
oh that, i am simply reusing the same variables within the class
Angius
Angiusā€¢2y ago
And your best bet would be not using that inner loop and the whole switch-case thing
Ash_
Ash_OPā€¢2y ago
i figured but i don't know any different way of doing that
Angius
Angiusā€¢2y ago
var thing = Suftrag.Auftfragliste.ElementAt(i);
var line = $"{thing.Gtin},{thing.Beizchung},{thing.Warengrope},{thing.Preis}";
tw.WriteLine(line);
var thing = Suftrag.Auftfragliste.ElementAt(i);
var line = $"{thing.Gtin},{thing.Beizchung},{thing.Warengrope},{thing.Preis}";
tw.WriteLine(line);
Angius
Angiusā€¢2y ago
This whole thing gets replaced
Angius
Angiusā€¢2y ago
And you could stand to use local variables instead of useless fields Keep the concerns of the method inside of the method
ero
eroā€¢2y ago
public static string AsCsv()
{
string fileName = $"{DateTime.Now:yyyy'-'MM'-'dd'_'HH':'mm}-Artikel.csv";
using var writer = new StreamWriter(Path.Join("..", "..", "Exports", fileName));

writer.WriteLine($"GTIN,Artikelnummer,Bezeichnung,Warengruppe,Preis");

for (int i = 0; i < Auftrag.Auftragsliste.Count; i++)
{
var auftrag = Auftrag.Auftragsliste[i];

writer.WriteLine($"{auftrag.Gtin},{auftrag.Artikelnummer},{auftrag.Bezeichnung},{auftrag.Warengruppe},{auftrag.Preis}");
}

return fileName;
}
public static string AsCsv()
{
string fileName = $"{DateTime.Now:yyyy'-'MM'-'dd'_'HH':'mm}-Artikel.csv";
using var writer = new StreamWriter(Path.Join("..", "..", "Exports", fileName));

writer.WriteLine($"GTIN,Artikelnummer,Bezeichnung,Warengruppe,Preis");

for (int i = 0; i < Auftrag.Auftragsliste.Count; i++)
{
var auftrag = Auftrag.Auftragsliste[i];

writer.WriteLine($"{auftrag.Gtin},{auftrag.Artikelnummer},{auftrag.Bezeichnung},{auftrag.Warengruppe},{auftrag.Preis}");
}

return fileName;
}
Just this right
Angius
Angiusā€¢2y ago
You missed writer.Close() But yeah Also, pretty sure
DateTime.Now.ToString(DateTime.Now.ToString("yyyy-MM-dd_HH:mm"))
DateTime.Now.ToString(DateTime.Now.ToString("yyyy-MM-dd_HH:mm"))
just won't work
MODiX
MODiXā€¢2y ago
Angius#1586
REPL Result: Success
DateTime.Now.ToString(DateTime.Now.ToString("yyyy-MM-dd_HH:mm"))
DateTime.Now.ToString(DateTime.Now.ToString("yyyy-MM-dd_HH:mm"))
Result: string
2022-12-15_07:26
2022-12-15_07:26
Compile: 448.697ms | Execution: 25.347ms | React with āŒ to remove this embed.
Angius
Angiusā€¢2y ago
Or maybe it will...
MODiX
MODiXā€¢2y ago
Angius#1586
REPL Result: Success
DateTime.Now.ToString("yyyy-MM-dd_HH:mm")
DateTime.Now.ToString("yyyy-MM-dd_HH:mm")
Result: string
2022-12-15_07:26
2022-12-15_07:26
Compile: 435.641ms | Execution: 26.537ms | React with āŒ to remove this embed.
Angius
Angiusā€¢2y ago
But so will just this lol So to modify Ero's code a little:
public static string AsCsv()
{
string fileName = DateTime.Now.ToString("yyyy-MM-dd_HH:mm")+"-Artikel.csv";
using var writer = new StreamWriter(Path.Join("..", "..", "Exports", fileName));

writer.WriteLine("GTIN,Artikelnummer,Bezeichnung,Warengruppe,Preis");

for (int i = 0; i < Auftrag.Auftragsliste.Count; i++)
{
var auftrag = Auftrag.Auftragsliste[i];

writer.WriteLine($"{auftrag.Gtin},{auftrag.Artikelnummer},{auftrag.Bezeichnung},{auftrag.Warengruppe},{auftrag.Preis}");
}

writer.Close();
return fileName;
}
public static string AsCsv()
{
string fileName = DateTime.Now.ToString("yyyy-MM-dd_HH:mm")+"-Artikel.csv";
using var writer = new StreamWriter(Path.Join("..", "..", "Exports", fileName));

writer.WriteLine("GTIN,Artikelnummer,Bezeichnung,Warengruppe,Preis");

for (int i = 0; i < Auftrag.Auftragsliste.Count; i++)
{
var auftrag = Auftrag.Auftragsliste[i];

writer.WriteLine($"{auftrag.Gtin},{auftrag.Artikelnummer},{auftrag.Bezeichnung},{auftrag.Warengruppe},{auftrag.Preis}");
}

writer.Close();
return fileName;
}
ero
eroā€¢2y ago
No i didn't I added a using to the writer
Ash_
Ash_OPā€¢2y ago
it's a csv hence i need the seperators
public static string AsCsv()
{
string filename = DateTime.Now.ToString(DateTime.Now.ToString("yyyy-MM-dd_HH:mm"))+"-Artikel.csv";
TextWriter tw = new StreamWriter("../../Exports/"+filename);
tw.WriteLine("GTIN" + Stringseperator + "Artikelnummer" + Stringseperator + "Bezeichnung"
+ Stringseperator + "Warengruppe" + Stringseperator + "Preis");
for (int i = -1; i < Auftrag.Auftragsliste.Count; i++)
{
Artikel thing = Auftrag.Auftragsliste.ElementAt(i);
string line = thing.Gtin + Stringseperator + thing.Artikelnummer + Stringseperator + thing.Bezeichnung
+ Stringseperator + thing.Warengruppe + Stringseperator + thing.Preis;
tw.WriteLine(line);
}
tw.Close();
return filename;
}
public static string AsCsv()
{
string filename = DateTime.Now.ToString(DateTime.Now.ToString("yyyy-MM-dd_HH:mm"))+"-Artikel.csv";
TextWriter tw = new StreamWriter("../../Exports/"+filename);
tw.WriteLine("GTIN" + Stringseperator + "Artikelnummer" + Stringseperator + "Bezeichnung"
+ Stringseperator + "Warengruppe" + Stringseperator + "Preis");
for (int i = -1; i < Auftrag.Auftragsliste.Count; i++)
{
Artikel thing = Auftrag.Auftragsliste.ElementAt(i);
string line = thing.Gtin + Stringseperator + thing.Artikelnummer + Stringseperator + thing.Bezeichnung
+ Stringseperator + thing.Warengruppe + Stringseperator + thing.Preis;
tw.WriteLine(line);
}
tw.Close();
return filename;
}
Angius
Angiusā€¢2y ago
Ah, you're right, it's disposable Then sure, this will work
ero
eroā€¢2y ago
The separator won't change, will it?
Angius
Angiusā€¢2y ago
Although the separator for Comma Separated Values is usually... a comma
ero
eroā€¢2y ago
We just chose to hard code it as a comma Don't see the need for that variable And just make a method for that, please... if you really have to
Angius
Angiusā€¢2y ago
Also, since you write the headers before the loop now, use a foreach Judging by ElementAt() that Aufragenliste is an enumerable
Ash_
Ash_OPā€¢2y ago
no, but in germany because of some bs it's a semicolon, because it's a semicolon seperated value... oh wait...
Angius
Angiusā€¢2y ago
So a perfect use for a foreach
ero
eroā€¢2y ago
They use Count on it, it's a List
Ash_
Ash_OPā€¢2y ago
yeah it is a list
Angius
Angiusā€¢2y ago
A foreach would still be easier lol Or at least use an indexer Not ElementAt
ero
eroā€¢2y ago
static string ToCsvRow(params object?[] args)
{
return string.Join(Separator, args.Select(arg => arg.ToString());
}
static string ToCsvRow(params object?[] args)
{
return string.Join(Separator, args.Select(arg => arg.ToString());
}
If you seriously have to
Ash_
Ash_OPā€¢2y ago
hm?
ero
eroā€¢2y ago
I'm not sure the select is even nevessary
Angius
Angiusā€¢2y ago
It's a helper method to turn multiple values into a separator-separated string
MODiX
MODiXā€¢2y ago
Ero#1111
REPL Result: Success
$"{DateTime.Now:yyyy'-'MM'-'dd'_'HH':'mm}-Artikel.csv"
$"{DateTime.Now:yyyy'-'MM'-'dd'_'HH':'mm}-Artikel.csv"
Result: string
2022-12-15_07:37-Artikel.csv
2022-12-15_07:37-Artikel.csv
Compile: 434.503ms | Execution: 35.757ms | React with āŒ to remove this embed.
ero
eroā€¢2y ago
That's the one
Ash_
Ash_OPā€¢2y ago
why would i need that?
Angius
Angiusā€¢2y ago
To make life easier, instead of concatenating long names It's not needed per se Just helpful
ero
eroā€¢2y ago
You asked for ways to shorten things... That's why you need that. To disturb things. Shorten Auto correct
Angius
Angiusā€¢2y ago
Non-English code is disturbing enough mweh
Ash_
Ash_OPā€¢2y ago
i meant that, thanks for the other one
ero
eroā€¢2y ago
Yeah, what i said stands
Ash_
Ash_OPā€¢2y ago
instead of what should i use that?
ero
eroā€¢2y ago
public static string AsCsv()
{
string fileName = $"{DateTime.Now:yyyy'-'MM'-'dd'_'HH':'mm}-Artikel.csv";
using var writer = new StreamWriter(Path.Join("..", "..", "Exports", fileName));

writer.WriteLine(
ConvertToCsvRow(
"GTIN",
"Artikelnummer",
"Bezeichnung",
"Warengruppe",
"Preis"));

foreach (var auftrag in Auftrag.Auftragsliste)
{
writer.WriteLine(
ConvertToCsvRow(
auftrag.Gtin,
auftrag.Artikelnummer,
auftrag.Bezeichnung,
auftrag.Warengruppe,
auftrag.Preis));
}

return fileName;
}

string ConvertToCsvRow(params object?[] values)
{
return string.Join(Separator, values);
}
public static string AsCsv()
{
string fileName = $"{DateTime.Now:yyyy'-'MM'-'dd'_'HH':'mm}-Artikel.csv";
using var writer = new StreamWriter(Path.Join("..", "..", "Exports", fileName));

writer.WriteLine(
ConvertToCsvRow(
"GTIN",
"Artikelnummer",
"Bezeichnung",
"Warengruppe",
"Preis"));

foreach (var auftrag in Auftrag.Auftragsliste)
{
writer.WriteLine(
ConvertToCsvRow(
auftrag.Gtin,
auftrag.Artikelnummer,
auftrag.Bezeichnung,
auftrag.Warengruppe,
auftrag.Preis));
}

return fileName;
}

string ConvertToCsvRow(params object?[] values)
{
return string.Join(Separator, values);
}
This should do it
Ash_
Ash_OPā€¢2y ago
thank you very much, still trying variations to make it easier to understand, what does the params object?[] values do? Also it says that the .Join part is not found
ero
eroā€¢2y ago
The join is definitely found params object?[] just means you can pass anything from 0 to infinitely many arguments to the method (so ConvertToCsvRow() is just as valid as ConvertToCsvRow(1, "abc", null, DateTime.Now))
Ash_
Ash_OPā€¢2y ago
Sorry, Cannot resolve symbol join
ero
eroā€¢2y ago
Yeah you're on an old .net version... You'll need to use Path. What was it I'm blanking Path.Combine
Ash_
Ash_OPā€¢2y ago
should be using the .net framework 4.8.0?
ero
eroā€¢2y ago
You should be using .NET 7 Or at the very least, if you must stay on framework, 4.8.1
Ash_
Ash_OPā€¢2y ago
eh it works with combine so don't really give two lines about that .-. thank you all for helping, it works and is shorter c:
Accord
Accordā€¢2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server