C
C#2w ago
WRLD

✅ Reading .csv files correctly

hey im trying to read a .csv file correctly by sorting the different values etc insied of it, two of those values are revenue and price and im trying to add all that up into my varible but i ends up as 0 all the time does anyone know what im doing wrong the format of the csv file is like this: "Id,Buyer User Id,Date and Time,Location,Location Id,Universe Id,Universe,Asset Id,Asset Name,Asset Type,Hold Status,Revenue,Price"
C#
string[] values = line.Split(',');
if (values.Length >= 14)
{
string holdStatus = values[10];
decimal revenue = 0;
decimal price = 0;
if (decimal.TryParse(values[11], out revenue) && decimal.TryParse(values[12], out price))
{
if (holdStatus.Equals("Released", StringComparison.OrdinalIgnoreCase))
{
totalEarnedReleased += revenue;
totalSpentReleased += price;
releasedCount++;
}
else if (holdStatus.Equals("Held", StringComparison.OrdinalIgnoreCase))
{
totalEarnedHeld += revenue;
totalSpentHeld += price;
heldCount++;
}
C#
string[] values = line.Split(',');
if (values.Length >= 14)
{
string holdStatus = values[10];
decimal revenue = 0;
decimal price = 0;
if (decimal.TryParse(values[11], out revenue) && decimal.TryParse(values[12], out price))
{
if (holdStatus.Equals("Released", StringComparison.OrdinalIgnoreCase))
{
totalEarnedReleased += revenue;
totalSpentReleased += price;
releasedCount++;
}
else if (holdStatus.Equals("Held", StringComparison.OrdinalIgnoreCase))
{
totalEarnedHeld += revenue;
totalSpentHeld += price;
heldCount++;
}
26 Replies
Angius
Angius2w ago
First deserialize that CSV into a list of objects Then do with that list whatever you want, sorting, summing, aggregating, whatever
WRLD
WRLD2w ago
alright i'' try that
Angius
Angius2w ago
https://joshclose.github.io/CsvHelper/ this library might be useful
WRLD
WRLD2w ago
why cant i download CsVHelper? nvm got it i have no idea but this has completely fucked my program no litterally nothing at all works.. @ZZZZZZZZZZZZZZZZZZZZZZZZZ
Angius
Angius2w ago
Meaning? Errors? Unintended behaviour? What exactly did you do? Just installing this library broke things? Any other code you changed?
WRLD
WRLD2w ago
no i tried to use
No description
WRLD
WRLD2w ago
this crap and impelemnt it and boom litterally 0 works n o values gets added not even a simple + works
Angius
Angius2w ago
That is a class, yes What's the code that's supposed to work but doesn't?
WRLD
WRLD2w ago
it sssupposed to run through the .csv file and collect info such as how many sales, where the sales have been, revenue and price and it does litterally nothing nowww it doesnt update any values or anything
Jimmacle
Jimmacle2w ago
sounds like your code is wrong then
WRLD
WRLD2w ago
well without the classes it was working for the msot part so either im doing this class stuff wrong or idk
Jimmacle
Jimmacle2w ago
it's probably that did you read the documentation for how to use csvhelper?
WRLD
WRLD2w ago
^^ this was the only part of my code that wasnt working properly before now its everything yup
Jimmacle
Jimmacle2w ago
well, you did something wrong i use this library for csv data and it's fine
WRLD
WRLD2w ago
well how do you correctly sort the data?
Jimmacle
Jimmacle2w ago
if "nothing works" then you shouldn't be thinking about sorting right now the first step is to correctly load the data in the first place
WRLD
WRLD2w ago
wym "load" the data theres nothing to load theres data to go throguh through*
Jimmacle
Jimmacle2w ago
so why are you doing anything with a CSV if there is no data
WRLD
WRLD2w ago
jesus crist
Jimmacle
Jimmacle2w ago
the data you need to load is in the CSV
WRLD
WRLD2w ago
ill do it myself
Jimmacle
Jimmacle2w ago
suit yourself i guess
Pobiega
Pobiega2w ago
"load" in this context means to read the csv file into memory you cant go through it without loading it
Jimmacle
Jimmacle2w ago
i don't see any sorting in the posted code either, so there are some terminology issues happening
Pobiega
Pobiega2w ago
he left the thread it seems?
Jimmacle
Jimmacle2w ago
the server their loss