C
C#β€’3mo ago
only you know

how to move json datas to mysql

I have few datas which stored in user_activity.json I need to move them to the mysql and I need to use them in my controller. In screenshots you can see the type of data and also I created a model like this.
No description
No description
249 Replies
Pobiega
Pobiegaβ€’3mo ago
is this a one-time thing or on an interval or triggered via an event? Are you using EF Core for MySQL, or just straight ADO.NET? Regardless, most of the code will be similar. Deserialize the JSON, your models seem to be for that already, then map each event to the corresponding datatype for your MySQL table, and insert it the insert step might look a bit different depending on how you talk to the database
only you know
only you knowβ€’3mo ago
one time thing I think I have the data i'll import to the mysql and for example I'll use it for getting custom methods like user1's total activity of 1 month, how many logins logouts username changes. I'll use ado.net
Pobiega
Pobiegaβ€’3mo ago
then make a console app, that just does this in 3 steps step 1: deserialize the json step 2: map from json models to database models, if they differ at all (likely) step 3: insert to DB
only you know
only you knowβ€’3mo ago
but I need to do it as api
Pobiega
Pobiegaβ€’3mo ago
no it makes no sense as an API, its a one time thing
only you know
only you knowβ€’3mo ago
first I'll move everything to db then created my model and it'll goes like service and controller isn't like this? I won't add anything but i need to use these datas with my program
Pobiega
Pobiegaβ€’3mo ago
the database is a separate thing you know this right? its has a life of its own you can connect to the same database from multiple programs so you can have an API for accessing this data, and the console app for reading the JSON and inserting it
only you know
only you knowβ€’3mo ago
oh you say console for just moving datas to db
Pobiega
Pobiegaβ€’3mo ago
yes.
only you know
only you knowβ€’3mo ago
so my program doesn't need to contain these kinda codes got you now
Pobiega
Pobiegaβ€’3mo ago
yeah, its not part of the API. just uses the same data
only you know
only you knowβ€’3mo ago
so I'll follow this steps
Pobiega
Pobiegaβ€’3mo ago
πŸ‘
only you know
only you knowβ€’3mo ago
@Pobiega how does it look?
No description
No description
No description
Pobiega
Pobiegaβ€’3mo ago
looks okay. A bit too OOP for my tastes πŸ˜„ I'd personally have separates the concerns a bit, so that reading and writing were two distinct steps, but it does the exact same thing
only you know
only you knowβ€’3mo ago
anyway it doesn't work
MySqlConnector.MySqlException (0x80004005): Data truncated for column 'Action' at row 1
at MySqlConnector.Core.ServerSession.ReceiveReplyAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/ServerSession.cs:line 892
at MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) in /_/src/MySqlConnector/Core/ResultSet.cs:line 37
at MySqlConnector.MySqlDataReader.ActivateResultSet(CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 130
at MySqlConnector.MySqlDataReader.InitAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, IDictionary`2 cachedProcedures, IMySqlCommand command, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 483
at MySqlConnector.Core.CommandExecutor.ExecuteReaderAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/CommandExecutor.cs:line 56
at MySqlConnector.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlCommand.cs:line 309
at MySqlConnector.MySqlCommand.ExecuteNonQuery() in /_/src/MySqlConnector/MySqlCommand.cs:line 108
at DataImporter.DataImporter.ImportData() in C:\Users\aytug\source\repos\DataImporter\Program.cs:line 69
MySqlConnector.MySqlException (0x80004005): Data truncated for column 'Action' at row 1
at MySqlConnector.Core.ServerSession.ReceiveReplyAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/ServerSession.cs:line 892
at MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) in /_/src/MySqlConnector/Core/ResultSet.cs:line 37
at MySqlConnector.MySqlDataReader.ActivateResultSet(CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 130
at MySqlConnector.MySqlDataReader.InitAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, IDictionary`2 cachedProcedures, IMySqlCommand command, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 483
at MySqlConnector.Core.CommandExecutor.ExecuteReaderAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/CommandExecutor.cs:line 56
at MySqlConnector.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlCommand.cs:line 309
at MySqlConnector.MySqlCommand.ExecuteNonQuery() in /_/src/MySqlConnector/MySqlCommand.cs:line 108
at DataImporter.DataImporter.ImportData() in C:\Users\aytug\source\repos\DataImporter\Program.cs:line 69
Pobiega
Pobiegaβ€’3mo ago
you. uhh.. I dont think it handles enums for you so you gotta manually cast that enum to whatever format you want. int/string
only you know
only you knowβ€’3mo ago
I even tried like this
No description
Pobiega
Pobiegaβ€’3mo ago
cast the enum (int)evt.Action
only you know
only you knowβ€’3mo ago
MySqlConnector.MySqlException (0x80004005): Data truncated for column 'Action' at row 1
at MySqlConnector.Core.ServerSession.ReceiveReplyAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/ServerSession.cs:line 892
at MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) in /_/src/MySqlConnector/Core/ResultSet.cs:line 37
at MySqlConnector.MySqlDataReader.ActivateResultSet(CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 130
at MySqlConnector.MySqlDataReader.InitAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, IDictionary`2 cachedProcedures, IMySqlCommand command, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 483
at MySqlConnector.Core.CommandExecutor.ExecuteReaderAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/CommandExecutor.cs:line 56
at MySqlConnector.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlCommand.cs:line 309
at MySqlConnector.MySqlCommand.ExecuteNonQuery() in /_/src/MySqlConnector/MySqlCommand.cs:line 108
at DataImporter.DataImporter.ImportData() in C:\Users\aytug\source\repos\DataImporter\Program.cs:line 60
MySqlConnector.MySqlException (0x80004005): Data truncated for column 'Action' at row 1
at MySqlConnector.Core.ServerSession.ReceiveReplyAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/ServerSession.cs:line 892
at MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) in /_/src/MySqlConnector/Core/ResultSet.cs:line 37
at MySqlConnector.MySqlDataReader.ActivateResultSet(CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 130
at MySqlConnector.MySqlDataReader.InitAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, IDictionary`2 cachedProcedures, IMySqlCommand command, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 483
at MySqlConnector.Core.CommandExecutor.ExecuteReaderAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/CommandExecutor.cs:line 56
at MySqlConnector.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlCommand.cs:line 309
at MySqlConnector.MySqlCommand.ExecuteNonQuery() in /_/src/MySqlConnector/MySqlCommand.cs:line 108
at DataImporter.DataImporter.ImportData() in C:\Users\aytug\source\repos\DataImporter\Program.cs:line 60
still same i guess
only you know
only you knowβ€’3mo ago
No description
only you know
only you knowβ€’3mo ago
are there mistakes here?
No description
only you know
only you knowβ€’3mo ago
cmd.Parameters.AddWithValue("@Action", evt.Action.HasValue ? evt.Action.Value.ToString() : null);
cmd.Parameters.AddWithValue("@Action", evt.Action.HasValue ? evt.Action.Value.ToString() : null);
what do you think about this? it's working in this way
only you know
only you knowβ€’3mo ago
but I think I made mistake while mapping
No description
only you know
only you knowβ€’3mo ago
cuz I have 10 users and I want to collect their activities but there are so many Id's should I a column as UserId and keep the activity id still or change the all Id's as user Id
Pobiega
Pobiegaβ€’3mo ago
ah, seems as if your database is treating enums as strings?
only you know
only you knowβ€’3mo ago
yes but as you see I couldn't manage to classify activites by users
only you know
only you knowβ€’3mo ago
there are so many usernames but all of them belongs to 10 user so I need to add "Id" as userId right
No description
Pobiega
Pobiegaβ€’3mo ago
I dont know what data you have or not have mate thats on you
Waffles from Human Resources
Stop normalizing ISO timestamps. :SCgetoutofmyhead:
only you know
only you knowβ€’3mo ago
can you check why I'm getting null return
No description
No description
No description
Pobiega
Pobiegaβ€’3mo ago
? $details
MODiX
MODiXβ€’3mo ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
Pobiega
Pobiegaβ€’3mo ago
and please use Enum.Parse<T>
only you know
only you knowβ€’3mo ago
I added the whole values to db and tried to parse here my point is to get reports of last 30 days
No description
only you know
only you knowβ€’3mo ago
instead of
(Action)Enum.Parse(typeof(Action), reader["Action"].ToString())
(Action)Enum.Parse(typeof(Action), reader["Action"].ToString())
should I use
Enum.Parse(Action, reader["Action"].ToString())
Enum.Parse(Action, reader["Action"].ToString())
this?
only you know
only you knowβ€’3mo ago
here is one example from db.
ID | Username | Timestamp | Action | Detail | userId
ID | Username | Timestamp | Action | Detail | userId
No description
Pobiega
Pobiegaβ€’3mo ago
no, you should use the generic overload of Parse Parse<Action>
only you know
only you knowβ€’3mo ago
can you give me an example please?
Pobiega
Pobiegaβ€’3mo ago
nah gotta do some work on your own
only you know
only you knowβ€’3mo ago
so my mistake is this?
Pobiega
Pobiegaβ€’3mo ago
I don't know you have not explained your problem you just said something about a null
only you know
only you knowβ€’3mo ago
my problem is: I added the whole values to db and tried to parse here my point is to get reports of last 30 days but from controller when I send the userId it returns null.
Pobiega
Pobiegaβ€’3mo ago
null? or an empty list? I can't see a way this code ever returns null
Pobiega
Pobiegaβ€’3mo ago
No description
only you know
only you knowβ€’3mo ago
sorry empty list. I think the problem is my method can't read the userId that I sent what do you think
Pobiega
Pobiegaβ€’3mo ago
yeah, because you never add it as a parameter
leowest
leowestβ€’3mo ago
also
No description
No description
Pobiega
Pobiegaβ€’3mo ago
yeah
No description
Pobiega
Pobiegaβ€’3mo ago
this is fucked
leowest
leowestβ€’3mo ago
also
No description
Pobiega
Pobiegaβ€’3mo ago
jesus why are you using ADO
leowest
leowestβ€’3mo ago
im so lost do u understand what the numbers stand for in there right? they are 0 based index representation of your columns in the SELECT so 0 is an Id, if its int it should be read as int
only you know
only you knowβ€’3mo ago
don't ask me it's not optional man
leowest
leowestβ€’3mo ago
GetString(0) reading an int?
only you know
only you knowβ€’3mo ago
I gave the numbers based on here.
No description
only you know
only you knowβ€’3mo ago
I saw an usage like this and it works
Pobiega
Pobiegaβ€’3mo ago
wdym what.. what are you doing why is there both a userid and an id? oh wait, okay one is the PKey
leowest
leowestβ€’3mo ago
but you're reading Username as string on index 0 which is Id
Pobiega
Pobiegaβ€’3mo ago
jesus, put the PKey on top for the love of god
only you know
only you knowβ€’3mo ago
id is action's id userId is users id
Pobiega
Pobiegaβ€’3mo ago
and you are not selecting a userid you are selecting Id and Username
leowest
leowestβ€’3mo ago
then name things properly ActionId UserId anyway I dont want to overflow the conversation I will leave it to my boss Pobi πŸ˜›
Pobiega
Pobiegaβ€’3mo ago
nah feel free to take over, I should be working πŸ˜„
leowest
leowestβ€’3mo ago
I dont understand how that code is not throwing thou is that code wrapped in a try and catch somewhere? where u call the method
only you know
only you knowβ€’3mo ago
nope
only you know
only you knowβ€’3mo ago
it's the whole section
No description
leowest
leowestβ€’3mo ago
well can u $paste it?
MODiX
MODiXβ€’3mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
leowest
leowestβ€’3mo ago
I cannot copy paste images
only you know
only you knowβ€’3mo ago
BlazeBin - vdlugdbhxtze
A tool for sharing your source code with the world!
leowest
leowestβ€’3mo ago
also Event and UserActivity
only you know
only you knowβ€’3mo ago
BlazeBin - sgbfuwedkuwo
A tool for sharing your source code with the world!
leowest
leowestβ€’3mo ago
SELECT userId, = 0
Id, = 1
Username, = 2
Timestamp, = 3
Action, = 4
Details = 5
FROM UserActivity
where userId = @userId AND
Timestamp >= DATE_SUB(NOW(), INTERVAL 30 DAY)
SELECT userId, = 0
Id, = 1
Username, = 2
Timestamp, = 3
Action, = 4
Details = 5
FROM UserActivity
where userId = @userId AND
Timestamp >= DATE_SUB(NOW(), INTERVAL 30 DAY)
that much you understand right?
only you know
only you knowβ€’3mo ago
yeha yeah
leowest
leowestβ€’3mo ago
var userActivity = new UserActivity
{
userId = reader.GetInt32(0),
Id = reader.GetInt32(1),
Events = new List<Event>()

};
userActivity.Events.Add(new Event
{
Username = reader.GetString(0),
Timestamp = reader.GetDateTime(1),
Action = (Action)Enum.Parse(typeof(Action), reader["Action"].ToString()),
Details = reader.GetString(3)
});
var userActivity = new UserActivity
{
userId = reader.GetInt32(0),
Id = reader.GetInt32(1),
Events = new List<Event>()

};
userActivity.Events.Add(new Event
{
Username = reader.GetString(0),
Timestamp = reader.GetDateTime(1),
Action = (Action)Enum.Parse(typeof(Action), reader["Action"].ToString()),
Details = reader.GetString(3)
});
so you can see your getting the wrong index for Username and Timestamp and details?
only you know
only you knowβ€’3mo ago
yes I see now
leowest
leowestβ€’3mo ago
ok what is column type for Action in your database? int? string?
only you know
only you knowβ€’3mo ago
enum btw working rn after fixing indexes
leowest
leowestβ€’3mo ago
this is mysql? sql?
only you know
only you knowβ€’3mo ago
mysql
leowest
leowestβ€’3mo ago
ok so another issue is you're creating 1 useractivity and 1 event per row you're not creating 1 useractivity and a list of events
only you know
only you knowβ€’3mo ago
is this wrong way
No description
leowest
leowestβ€’3mo ago
so you have to initialize the useractivity outside the loop and fill it in yes because of what I just said you're reading row by row and useractivity is inside the loop so what happens when u create the object new every loop?
only you know
only you knowβ€’3mo ago
but in db it stored like this
No description
leowest
leowestβ€’3mo ago
while (await reader.ReadAsync())
{
creates a new useractivity
creates a new event and add to user activity
add user acitivity to list
}
while (await reader.ReadAsync())
{
creates a new useractivity
creates a new event and add to user activity
add user acitivity to list
}
this is essentially what you're doing every time the loop runs do u see why u have many useractivities instead of just 1 you need this
only you know
only you knowβ€’3mo ago
oh you say you have one user activity and lots of events right?
leowest
leowestβ€’3mo ago
instantiate new useractivity
while (await reader.ReadAsync())
{
updates useractivity info
creates a new event and add to user activity
}
add user acitivity to list
instantiate new useractivity
while (await reader.ReadAsync())
{
updates useractivity info
creates a new event and add to user activity
}
add user acitivity to list
no u have lots of both
only you know
only you knowβ€’3mo ago
yeah because it stores randomized datas for 1 year
leowest
leowestβ€’3mo ago
no
only you know
only you knowβ€’3mo ago
randomized number of logins logouts
leowest
leowestβ€’3mo ago
because u create a new user every row in your loop
MODiX
MODiXβ€’3mo ago
leowest
REPL Result: Success
var activities = new List<Activity>();
for (var i = 0; i < 5; i++)
{
var user = new Activity { Id = 1 };
var evt = new EventInfo { Id = 2 };
user.Events.Add(evt);
activities.Add(user);
}

foreach (var item in activities)
{
Console.Write($"{item.Id} => ");
foreach (var e in item.Events)
{
Console.WriteLine($"{e.Id}");
}
}

public class Activity
{
public int Id {get;set;}
public List<EventInfo> Events {get;set;} = new List<EventInfo>();
}

public class EventInfo
{
public int Id {get;set;}
}
var activities = new List<Activity>();
for (var i = 0; i < 5; i++)
{
var user = new Activity { Id = 1 };
var evt = new EventInfo { Id = 2 };
user.Events.Add(evt);
activities.Add(user);
}

foreach (var item in activities)
{
Console.Write($"{item.Id} => ");
foreach (var e in item.Events)
{
Console.WriteLine($"{e.Id}");
}
}

public class Activity
{
public int Id {get;set;}
public List<EventInfo> Events {get;set;} = new List<EventInfo>();
}

public class EventInfo
{
public int Id {get;set;}
}
Console Output
1 => 2
1 => 2
1 => 2
1 => 2
1 => 2
1 => 2
1 => 2
1 => 2
1 => 2
1 => 2
Quoted by
<@1102729783969861782> from #bot-spam (click here)
Compile: 575.945ms | Execution: 76.195ms | React with ❌ to remove this embed.
leowest
leowestβ€’3mo ago
so u see how it prints 1 => 2 for 5 times in different lines? that means you have 5 different instances of Activity user each with 1 event and not 1 Activity with a list of 5 events because every time u create a new user inside the loop
MODiX
MODiXβ€’3mo ago
leowest
instantiate new useractivity
while (await reader.ReadAsync())
{
updates useractivity info
creates a new event and add to user activity
}
add user acitivity to list
instantiate new useractivity
while (await reader.ReadAsync())
{
updates useractivity info
creates a new event and add to user activity
}
add user acitivity to list
Quoted by
React with ❌ to remove this embed.
only you know
only you knowβ€’3mo ago
Oh, now I understand what are you mean thank you. It's a bit confusing
leowest
leowestβ€’3mo ago
well just ask where u dont understand
only you know
only you knowβ€’3mo ago
no I mean the general of ado
leowest
leowestβ€’3mo ago
I mean that is not ado related its pure c# you're instantiating an object inside the loop so it will be a new object every iteration there are times u may want that, but not right now u due to how u want to fill your 2 classes
only you know
only you knowβ€’3mo ago
I need to instantiate a object out of loop and in the loop I'll take every data and insert it to my object am I right
leowest
leowestβ€’3mo ago
just like that once u write the changes post the code here, preferable as code not image pls πŸ˜› and I will look at it again
only you know
only you knowβ€’3mo ago
how do I update useractivity info?
leowest
leowestβ€’3mo ago
inside the loop
only you know
only you knowβ€’3mo ago
so I instantiate new useractivity out of loop but couldn't find how to update inside in it
leowest
leowestβ€’3mo ago
yes just the class then use inside the loop user.Id = ..., user.UserId = ... also initialize user.Events = new List<Event>() outside
only you know
only you knowβ€’3mo ago
BlazeBin - epkelpilxdpm
A tool for sharing your source code with the world!
only you know
only you knowβ€’3mo ago
No description
leowest
leowestβ€’3mo ago
the code in the link is old
only you know
only you knowβ€’3mo ago
BlazeBin - pxwmdgqylmpt
A tool for sharing your source code with the world!
leowest
leowestβ€’3mo ago
looks good πŸ˜‰ is that the desired json? also u dont need to do this reader["Action"].ToString() u can just do reader.GetString(4) fairly sure the enums are read as strings in mysql.data
only you know
only you knowβ€’3mo ago
thank you for your help really yes that was what I want
leowest
leowestβ€’3mo ago
no worries that's what we are here for learn and help πŸ˜‰
only you know
only you knowβ€’3mo ago
I converted it to the example: user2 logged in 15 times logged out 20 times now I gotta do it same thing for all users at the same time but I learned how to do itπŸ™‚ and after that I need to learn how to convert it to json and store it in json file backwards I mean
leowest
leowestβ€’3mo ago
deserializing is the same thing if u serialized List<UserActivity> then u deserialize that
only you know
only you knowβ€’3mo ago
i deserialized it so i gotta serialize with same steps?
leowest
leowestβ€’3mo ago
u serialized it serialize is when u generate the json deserialize is when u read back into the object
only you know
only you knowβ€’3mo ago
oh I thought I deserialized it for json to db
leowest
leowestβ€’3mo ago
nah
only you know
only you knowβ€’3mo ago
quite opposite then
leowest
leowestβ€’3mo ago
althou there are database u can insert json directly into a column and they have a feature that lets u json search the column so its not just text inside a column
only you know
only you knowβ€’3mo ago
I see, when time comes for serialize I'll deeply search thank you
leowest
leowestβ€’3mo ago
$close
MODiX
MODiXβ€’3mo ago
Use the /close command to mark a forum thread as answered
only you know
only you knowβ€’3mo ago
I'll come to end of my school can I ask you some things about what should I do after school? is it propriate ask you from dm
leowest
leowestβ€’3mo ago
I dont take dms sorry, you better ask on #career-talk or u could also open a new #help with the new subject im sure people would be whiling to share what they can
Pobiega
Pobiegaβ€’3mo ago
What level of school are we talking? I wouldn't quite say you're job ready just yet and need a bit more practice. Best way to get that is to just make stuff, or go some kind of apprenticeship if possible.
only you know
only you knowβ€’3mo ago
university I'm thinking exactly same thing. I'm doing volunteerly internship right now. They giving me tasks and I'm trying to do it with learning as well. and I think I need to practice more than bit😏
leowest
leowestβ€’3mo ago
u need to also learn a bit more of problem solving your self like the loop thing was something simple u could have figured your self πŸ™‚ problem solving is the most important skill to acquire
only you know
only you knowβ€’3mo ago
you're right. I'm looking for help every little problem. I should stop doing that
leowest
leowestβ€’3mo ago
also learn to $debug
MODiX
MODiXβ€’3mo ago
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
leowest
leowestβ€’3mo ago
this is very very very important if u see its not working the way u expect breakpoint step thru the code line by line see where something undesired happens think why etc so if u had debugged that loop u would have seen than when useractivity is inserted to the list what it looks like u can hover the mouse see the contents etc
only you know
only you knowβ€’3mo ago
that't right I'll try to be more careful from now and also I'll check the link I'm going home rn today was tiring to me😫
leowest
leowestβ€’3mo ago
specially when you're writing complex things that can be a life saving or even when something breaks or throws because u can evaluate the values and see do u have 0 try / catch in your code? I am still not convinced that your code with wrong indexes was not crashing your app
only you know
only you knowβ€’3mo ago
sometimes I can understand my mistakes but can't figure out what to do after it my semi magical fingers πŸ˜›
leowest
leowestβ€’3mo ago
and that's where thinking comes in hand, experimenting, if its something u haven't learned u won't figure out but if its something u did u will there is absolute 0 try catch in your entire code?
only you know
only you knowβ€’3mo ago
nope
leowest
leowestβ€’3mo ago
im amazed at how the library doesn't throw then, garbage mysql
only you know
only you knowβ€’3mo ago
is thar make sense that doing everything in try catch or just when I expect errors? yh you're right about that but sometimes I can forget things that I know
leowest
leowestβ€’3mo ago
imo for learning u should never use try and catch they can often hide the real problem because they eat the exception that could have happened to tell u the issue
only you know
only you knowβ€’3mo ago
yea seeing the real problem is way better I think
leowest
leowestβ€’3mo ago
there are cases u might need try catch or is forced to used it thou like for example I was writing some maui and the throw error had literally no useful information when I wrapped it in a try and catch the error was completely different which leaded me to the answer so these are some special cases I guess
only you know
only you knowβ€’3mo ago
whats your job at software i mean like backend front end
leowest
leowestβ€’3mo ago
both except for web I dont yet use asp.net mainly php unhappily I do make some internal apis in asp.net thou
only you know
only you knowβ€’3mo ago
are you working or student like me( i dont think ur student thou)
leowest
leowestβ€’3mo ago
working self employeed thou and @Pobiega owns a bank in sweden
only you know
only you knowβ€’3mo ago
did you choose to be self? kidding🀯
leowest
leowestβ€’3mo ago
no, its just circustances of life unhappily
only you know
only you knowβ€’3mo ago
but some things are working in their way right? that's something imo hey @leowest can I ask you a question?
leowest
leowestβ€’3mo ago
u just did
only you know
only you knowβ€’3mo ago
in here I'm trying to count total userId's but I couldn't manage to do it
No description
only you know
only you knowβ€’3mo ago
it counting just 1 when I tried it has to be 10 let me send my code like
only you know
only you knowβ€’3mo ago
BlazeBin - wzcngjmwcqza
A tool for sharing your source code with the world!
leowest
leowestβ€’3mo ago
breakpoint on the foreach check the content of usersActivities see if the content is what u expect
only you know
only you knowβ€’3mo ago
my content is in userActivity its like userActivity.userId
foreach (var userActivity in usersActivities)
{
userActivity.userId++;
userActivityReport.userId = userActivity.userId;
foreach (var userActivity in usersActivities)
{
userActivity.userId++;
userActivityReport.userId = userActivity.userId;
I tried to add this but counts 2 oh I understand why its 2 its not counting its just adding 1 to userId 1
leowest
leowestβ€’3mo ago
u said u want to figure things out, so u debug breakpoint and step in is the easiest way to see what your code is doing with what data and then u ask is this what I wanted, no? then what did u want that its not doing so u can see X is increasing, Y is increasing Z is increasing but B is not the worst part is that if the userId was 10 u would believe it was adding πŸ˜›
only you know
only you knowβ€’3mo ago
no userId is 1 total user number 10
leowest
leowestβ€’3mo ago
I am saying IF it was because u would had the ilusion it was ++
only you know
only you knowβ€’3mo ago
then why prints 2
leowest
leowestβ€’3mo ago
total user number from where? I dont know what useracitivities have does it have 1 item with many events? does it have many items with many events?
only you know
only you knowβ€’3mo ago
its like this I have 10 user and they have many events and many actions
No description
leowest
leowestβ€’3mo ago
im not referring to the class im referring to what is the content that is why u breakpoint so u can see if the content is right if its what u expect and if it would have the count u expect
only you know
only you knowβ€’3mo ago
oh wait
leowest
leowestβ€’3mo ago
userActivity.userId++;
userActivityReport.userId = userActivity.userId;
userActivity.userId++;
userActivityReport.userId = userActivity.userId;
this doesn't make sense either you're increasing the userId from useractivity and not the report maybe u need
public class UserActivityReport
{
public int userId { get; set; }
public int Count { get; set; }
public int LoginCount { get; set; }
public int LogoutCount { get; set; }
public int UsernameChangedCount { get; set; }
}
public class UserActivityReport
{
public int userId { get; set; }
public int Count { get; set; }
public int LoginCount { get; set; }
public int LogoutCount { get; set; }
public int UsernameChangedCount { get; set; }
}
where count is the actual userId count? but since I dont know the content of useractivities at that moment I can't really tell
Pobiega
Pobiegaβ€’3mo ago
just for the record, this should be three queries to the database instead of fetching all the records and doing it in-app databases are very good at their jobs. they count a lot faster than your app can do
leowest
leowestβ€’3mo ago
its also a very good opportunity for him to use the debug thou
only you know
only you knowβ€’3mo ago
so you're saying I shouldn't make the count here just let the count db
Pobiega
Pobiegaβ€’3mo ago
I am. but leo has a point learn to use the debugger, then fix it πŸ˜„
leowest
leowestβ€’3mo ago
mainly why I didnt mention much ;x I was waiting to see if he would figure out what is wrong and then crush his dreams last part was a joke btw
only you know
only you knowβ€’3mo ago
figuring the let db does this job? or debugging np you can have fun with me thou
leowest
leowestβ€’3mo ago
you can do what your doing in code and with the db I first wanted u to debug to see what your doing wrong in code and then explain that u can also do it from db which would effectively give u the List<UserActivityReport> as an output or just UserActivityReport if its just 1 user but u said u wanted to figure things out more often and solve problem so that issue sounded like the perfect example that u could solve your self and debuging should tell u what is wrong or missing
only you know
only you knowβ€’3mo ago
but I'm doing the count in app because im using the data which comes from db is that matter what I mean is im takin the whole data with repo from db and then im editing the data from service is it wrong
leowest
leowestβ€’3mo ago
forget about the db for now just try to figure out why its not counting
only you know
only you knowβ€’3mo ago
okay let me try to fix that first
leowest
leowestβ€’3mo ago
its not wrong in some scenarios u may need to count in code and it might be faster
only you know
only you knowβ€’3mo ago
btw do you know the solution
leowest
leowestβ€’3mo ago
I have assumptions of what it could be, some of which I already pointed above but without knowing the content it would be hard to say
only you know
only you knowβ€’3mo ago
ok ok I will solve it promise don't help me
leowest
leowestβ€’3mo ago
:catlaugh: godspeed
public async Task<UserActivityReport> GetUserActivityReport(int userId)
{
var userActivityReport = new UserActivityReport();
var userActivities = await _userActivityRepository.ReportOfOneMonth(userId);
foreach (var userActivity in userActivities)
{
foreach (var evt in userActivity.Events)
{
switch (evt.Action)
{
case Action.Login:
userActivityReport.LoginCount++;
break;
case Action.Logout:
userActivityReport.LogoutCount++;
break;
case Action.UsernameChanged:
userActivityReport.UsernameChangedCount++;
break;
default:
break;
}
}
userActivityReport.userId = userActivity.userId;
}
return userActivityReport;
}
public async Task<UserActivityReport> GetUserActivityReport(int userId)
{
var userActivityReport = new UserActivityReport();
var userActivities = await _userActivityRepository.ReportOfOneMonth(userId);
foreach (var userActivity in userActivities)
{
foreach (var evt in userActivity.Events)
{
switch (evt.Action)
{
case Action.Login:
userActivityReport.LoginCount++;
break;
case Action.Logout:
userActivityReport.LogoutCount++;
break;
case Action.UsernameChanged:
userActivityReport.UsernameChangedCount++;
break;
default:
break;
}
}
userActivityReport.userId = userActivity.userId;
}
return userActivityReport;
}
Just going back to what I already said: - what is the content of userActivities - why are you setting userActivityReport.userId = userActivity.userId; is that right? - what is the total count u mentioned above is that suppose to be the userId in userActivityReport or is that supposed to be an extra property u didnt add to it if you breakpoint and step in line by line u can see what is going on, what the code is doing with userActivities and what is missing
MODiX
MODiXβ€’3mo ago
only you know
total user number 10
Quoted by
React with ❌ to remove this embed.
only you know
only you knowβ€’3mo ago
its the wrong function you know that right
leowest
leowestβ€’3mo ago
ah its the one under?
only you know
only you knowβ€’3mo ago
yh the one you sent is calculating for just 1 user with id
leowest
leowestβ€’3mo ago
sure but you're not counting users on the 2nd one
only you know
only you knowβ€’3mo ago
yeah im trying to deal with it rn
leowest
leowestβ€’3mo ago
so basically you are missing a property or is userId suppose to be the count? because the naming would be awful
only you know
only you knowβ€’3mo ago
I know im aware
leowest
leowestβ€’3mo ago
ok just clearing that up
only you know
only you knowβ€’3mo ago
I used same property in 2 different methods just being lazy 🫣
leowest
leowestβ€’3mo ago
either way what I said above still stands the logic of what u need to debug only thing is u not increasing users count on the 2nd method
only you know
only you knowβ€’3mo ago
I said im trying to increase it but couldn't figured out in which loop as I understand second loop works till counting is done but I cant count my users there because it's not an action
leowest
leowestβ€’3mo ago
well first loop walk thru all users second loop walks thru all events of a given user given your sample json
only you know
only you knowβ€’3mo ago
that's right okay let me try somethings again
leowest
leowestβ€’3mo ago
userActivities.Count is the total users u have in useractivities
only you know
only you knowβ€’3mo ago
how do I use it
leowest
leowestβ€’3mo ago
how do u want to use it? that is literally a property
only you know
only you knowβ€’3mo ago
wdym how do I want
leowest
leowestβ€’3mo ago
public async Task<UserActivityReport> GetReportsOfAllUsers()
{
var userActivityReport = new UserActivityReport();
var usersActivities = await _userActivityRepository.GetAllActivity();
foreach (var userActivity in usersActivities)
{
foreach(var evt in userActivity.Events)
{
switch (evt.Action)
{
case Action.Login:
userActivityReport.LoginCount++;
break;
case Action.Logout:
userActivityReport.LogoutCount++;
break;
case Action.UsernameChanged:
userActivityReport.UsernameChangedCount++;
break;
default:
break;
}
}
}
return userActivityReport;
}
public async Task<UserActivityReport> GetReportsOfAllUsers()
{
var userActivityReport = new UserActivityReport();
var usersActivities = await _userActivityRepository.GetAllActivity();
foreach (var userActivity in usersActivities)
{
foreach(var evt in userActivity.Events)
{
switch (evt.Action)
{
case Action.Login:
userActivityReport.LoginCount++;
break;
case Action.Logout:
userActivityReport.LogoutCount++;
break;
case Action.UsernameChanged:
userActivityReport.UsernameChangedCount++;
break;
default:
break;
}
}
}
return userActivityReport;
}
there is no where in here to save a total count what is missing? do u want to save it to userId?
only you know
only you knowβ€’3mo ago
temp variable?
leowest
leowestβ€’3mo ago
ok and do what with it im trying to understand what u want to do with it? does it need to be in your report? do u want to it for something inside the loops?
only you know
only you knowβ€’3mo ago
increase +1 in every userId count
leowest
leowestβ€’3mo ago
but u already have the total do u have repeated userId? userActivityReport.userId = usersActivities.Count; if that is what u want to do
only you know
only you knowβ€’3mo ago
I guess so
leowest
leowestβ€’3mo ago
if u dont use count count the first loop rr u just ++ the userId in the first loop
only you know
only you knowβ€’3mo ago
my point is count every action also count user Id and I'll use it for report
leowest
leowestβ€’3mo ago
would be the same outcome
only you know
only you knowβ€’3mo ago
its like 10 users 10 times logged in
leowest
leowestβ€’3mo ago
yes, if each user logged in once
only you know
only you knowβ€’3mo ago
nope its like 1 month of data each user almost logged in more than 100
leowest
leowestβ€’3mo ago
then that is my point ur content is not right if its counting only 10 logged in times
only you know
only you knowβ€’3mo ago
I don't understand how it's wrong it's working fine what's wrong thing about it
leowest
leowestβ€’3mo ago
then why its not giving u 100?
only you know
only you knowβ€’3mo ago
what why do I need 100 for_?
leowest
leowestβ€’3mo ago
:catfacepalm:
only you know
only you knowβ€’3mo ago
wait let me show somethign
only you know
only you knowβ€’3mo ago
0 is usercount and login, logout, usernamechanged
No description
leowest
leowestβ€’3mo ago
ok so you want to get the total amount of users registered?
only you know
only you knowβ€’3mo ago
I just want to count total userId's that containing in data there is no register
leowest
leowestβ€’3mo ago
data being useractivities?
only you know
only you knowβ€’3mo ago
yes
leowest
leowestβ€’3mo ago
and useractivities does not have duplicated userId?
only you know
only you knowβ€’3mo ago
nope
leowest
leowestβ€’3mo ago
so yes userActivities.Count is equal to usercount now if its 1, 2, 10, 100 and that number is not right then there is something not right with how userAcitivities get its data
only you know
only you knowβ€’3mo ago
how do I use useractivities.count
leowest
leowestβ€’3mo ago
do u understand that? U have List<UserActivities> with no repeated user id List<> have a property called Count https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.count?view=net-8.0
only you know
only you knowβ€’3mo ago
I understand that
only you know
only you knowβ€’3mo ago
am I stupid?
No description
leowest
leowestβ€’3mo ago
no () and outside foreach
only you know
only you knowβ€’3mo ago
its not allowing me to use it without ()
leowest
leowestβ€’3mo ago
what type is userActitivties userActivities[]? if so then .Length
only you know
only you knowβ€’3mo ago
No description
leowest
leowestβ€’3mo ago
$typehints
MODiX
MODiXβ€’3mo ago
Visual Studio 2019 and above can now give you inline hints of the type in lambda, var, etc, see the animated GIF on how to enable it: https://cdn.discordapp.com/attachments/169726586931773440/869498806343962694/2021-07-27_05-35-23.gif See the below image for an example of what it looks like the white line was made by me to illustrate what the above does: https://media.discordapp.net/attachments/569261465463160900/894398422365372416/unknown.png NOTE: You can also use it by pressing ALT + F1! You might need to go on the settings and disable/reenable it for it to work!
leowest
leowestβ€’3mo ago
if u hover your mouse over GetAllActivity it iwll tell u but I looked at your code from before its IEnumerable so in this case yes u would be doing userActivityReport.userId++; in the first loop
only you know
only you knowβ€’3mo ago
No description
only you know
only you knowβ€’3mo ago
sorry I am doing it just prints 1
leowest
leowestβ€’3mo ago
ok god why do u not want to breakpoint? do u understand that if it count 1 u only have 1 entry? and I've been saying from the begin to debug the content
only you know
only you knowβ€’3mo ago
I did
leowest
leowestβ€’3mo ago
and did u hover the mouse over useractivities
only you know
only you knowβ€’3mo ago
yes ffs
leowest
leowestβ€’3mo ago
ok and did u expand it to see how many items it had did u see more than 1
only you know
only you knowβ€’3mo ago
what is item
No description
leowest
leowestβ€’3mo ago
I can't see the tree
only you know
only you knowβ€’3mo ago
tree?
leowest
leowestβ€’3mo ago
screenshot too small it doesn't show the outside so I can't see the elements
only you know
only you knowβ€’3mo ago
No description
leowest
leowestβ€’3mo ago
No description
leowest
leowestβ€’3mo ago
u have 1 item so its only right that your count will be 1 we spent all this time because u didn't want to read the debug link I sent u πŸ˜‰ it shows all that shows how to look at the element shows how to step in
only you know
only you knowβ€’3mo ago
okay can you stop thank you for your help anyway
Want results from more Discord servers?
Add your server
More Posts