ā 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?
53 Replies
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.
What's with the loop starting at -1, and using fields?
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
It's not got anything to do with English, fields are just a general concept in C#
could you reference the line(s) for this? i code mostly in german and don't know what a field is regarding c#?
i code mostly in germanš $structure
For C# versions older than 10, see
$StructureOld
oh that, i am simply reusing the same variables within the class
And your best bet would be not using that inner loop and the whole switch-case thing
i figured but i don't know any different way of doing that
This whole thing gets replaced
And you could stand to use local variables instead of useless fields
Keep the concerns of the method inside of the method
Just this right
You missed
writer.Close()
But yeah
Also, pretty sure
just won't workAngius#1586
REPL Result: Success
Result: string
Compile: 448.697ms | Execution: 25.347ms | React with ā to remove this embed.
Or maybe it will...
Angius#1586
REPL Result: Success
Result: string
Compile: 435.641ms | Execution: 26.537ms | React with ā to remove this embed.
But so will just this lol
So to modify Ero's code a little:
No i didn't
I added a
using
to the writerit's a csv hence i need the seperators
Ah, you're right, it's disposable
Then sure, this will work
The separator won't change, will it?
Although the separator for Comma Separated Values is usually... a comma
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
Also, since you write the headers before the loop now, use a foreach
Judging by
ElementAt()
that Aufragenliste is an enumerableno, but in germany because of some bs it's a semicolon, because it's a semicolon seperated value... oh wait...
So a perfect use for a foreach
They use Count on it, it's a List
yeah it is a list
A foreach would still be easier lol
Or at least use an indexer
Not ElementAt
If you seriously have to
hm?
I'm not sure the select is even nevessary
It's a helper method to turn multiple values into a separator-separated string
Ero#1111
REPL Result: Success
Result: string
Compile: 434.503ms | Execution: 35.757ms | React with ā to remove this embed.
That's the one
why would i need that?
To make life easier, instead of concatenating long names
It's not needed per se
Just helpful
You asked for ways to shorten things...
That's why you need that. To disturb things.
Shorten
Auto correct
Non-English code is disturbing enough
i meant that, thanks for the other one
Yeah, what i said stands
instead of what should i use that?
This should do it
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 foundThe 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)
)Sorry,
Cannot resolve symbol join
Yeah you're on an old .net version...
You'll need to use Path.
What was it
I'm blanking
Path.Combine
should be using the .net framework 4.8.0?
You should be using .NET 7
Or at the very least, if you must stay on framework, 4.8.1
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:
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.