C
C#3y ago
İrşat

Foreach vs Count with likes table

Guys I made a Vote(likes and dislikes) table. It's similar to reddit's karma system. Dislikes decrease the likes. So far so good but now I am using foreach to check if vote is True or False and do the calculation. Would it be faster if i made a two different table and just use likes.Count - dislikes.Count instead?
50 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
sql table mssql
public List<Vote> votes { get; set; } (inside Post model)

public class Vote
{
public int accountId { get; set; }
public bool body { get; set; }
}
public List<Vote> votes { get; set; } (inside Post model)

public class Vote
{
public int accountId { get; set; }
public bool body { get; set; }
}
int likes = 0;
foreach(Vote value in post.votes){
if(value.body == true)
likes++;
else
likes--;
}
int likes = 0;
foreach(Vote value in post.votes){
if(value.body == true)
likes++;
else
likes--;
}
Front end
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
then two different tables for likes and dislikes
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
yes but that's not api. It's my controller and its models there are more columns in vote
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
how do i do that in sql database? another column perhaps?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
no i mean the calculation. How do I do that in database? I thought you meant calculating and writing it in an another column
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
in a moment it froze lol, a minute more
İrşat
İrşatOP3y ago
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
yup
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
do you mean api?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
finally, i was trying to understand why you was talking about database calculation
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
btw, i am returning a list of post like this;
var posts = await mapper.ProjectTo<PostDtoRead_1>(db.Posts.Where(p =>
p.IsPublished == true &&
p.DeletedStatus!.Body == "Default")).ToListAsync();
var posts = await mapper.ProjectTo<PostDtoRead_1>(db.Posts.Where(p =>
p.IsPublished == true &&
p.DeletedStatus!.Body == "Default")).ToListAsync();
i guess i have to add that code into the dto
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
wait a min I am confused 😄 will postdto have a custom property for the karma value? do I calculate it in constructer or something?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
i am not sure if i want to use linq or not 😄
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
public class PostDtoRead_1
{
public int Id { get; set; }
public string Title { get; set; } = null!;
public string PostDescription { get; set; } = null!;
public DateTime PublishDate { get; set; }
public DateTime UpdateDate { get; set; }
public string? CoverImage { get; set; }

public AccountDtoRead_1? Account { get; set; }
public DeletedStatusDto? DeletedStatus { get; set; }
public LanguageDto? Language { get; set; }
public PostStatusDto? PostStatus { get; set; }
public PostTypeDto? PostType { get; set; }
public RatedAsDto? RatedAs { get; set; }
//-----------
public ICollection<ChapterDtoRead_1>? Chapters { get; set; }
public ICollection<CommentDtoRead_1>? Comments { get; set; }
//-----------
public ICollection<PostGiftDto>? PostGifts { get; set; }
public ICollection<RateDto>? Rates { get; set; }
public ICollection<VoteDto>? Votes { get; set; }
public ICollection<TagDto>? Tags { get; set; }
public ICollection<ExistingStoryDto>? ExistingStories { get; set; }

public int Karma { get; set; }

public PostDtoRead_1(ICollection<VoteDto>? Votes)
{
this.Karma = Votes.Count(l => l.Body == true) - Votes.Count(d => d.Body == false);
}
}
public class PostDtoRead_1
{
public int Id { get; set; }
public string Title { get; set; } = null!;
public string PostDescription { get; set; } = null!;
public DateTime PublishDate { get; set; }
public DateTime UpdateDate { get; set; }
public string? CoverImage { get; set; }

public AccountDtoRead_1? Account { get; set; }
public DeletedStatusDto? DeletedStatus { get; set; }
public LanguageDto? Language { get; set; }
public PostStatusDto? PostStatus { get; set; }
public PostTypeDto? PostType { get; set; }
public RatedAsDto? RatedAs { get; set; }
//-----------
public ICollection<ChapterDtoRead_1>? Chapters { get; set; }
public ICollection<CommentDtoRead_1>? Comments { get; set; }
//-----------
public ICollection<PostGiftDto>? PostGifts { get; set; }
public ICollection<RateDto>? Rates { get; set; }
public ICollection<VoteDto>? Votes { get; set; }
public ICollection<TagDto>? Tags { get; set; }
public ICollection<ExistingStoryDto>? ExistingStories { get; set; }

public int Karma { get; set; }

public PostDtoRead_1(ICollection<VoteDto>? Votes)
{
this.Karma = Votes.Count(l => l.Body == true) - Votes.Count(d => d.Body == false);
}
}
I did a thing like that. Last part is completely random, I just added it for reasons 😄 anyway, i am gonna eat something, i will be back in 20 min if you want to say something, thanks for the help!
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
I use body for the main focus on tables. Body can be the text in comment for example Would this even work? I have no idea
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
no i don't mean your idea the constructer
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
yeah i didn't do it for that exact reason whose vote is this? you know you can add an another many to many table to tell that user voted this post already. But at this point I can just use a vote table wait a minute it worked lol i feel like crazy scientist it even worked for the individual posts which uses this dto with inheritence
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
3?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
is it because sql uses bit?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
was 3 True?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
not sure i am following it's bool
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3y ago
tebeco#0205
post.votes.Count(v => v.Body)
Quoted by
React with ❌ to remove this embed.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
this is the first time i am hearing about this 😄 oh the logic you mean body returns true or false why 3?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
ooohhhh, I thought we was talking about v => v.Body == 3 or something
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşatOP3y ago
😄 see you okay case closed. Thanks @Eugene not you tobeclone, you confused me for 5 minutes 😠 even if it's my fault
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?