C#
C#

help

Root Question Message

Lary
Lary9/22/2022
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?
Message Not Public

Sign In and Join Server To See

9/22/2022
Lary
Lary9/22/2022
sql table
Lary
Lary9/22/2022
mssql
Lary
Lary9/22/2022
public List<Vote> votes { get; set; }  (inside Post model)

public class Vote
{
    public int accountId { get; set; }
    public bool body { get; set; }
}
Lary
Lary9/22/2022
int likes = 0;
foreach(Vote value in post.votes){
    if(value.body == true)
          likes++;
    else
          likes--;
}
Front end
Message Not Public

Sign In and Join Server To See

9/22/2022
Lary
Lary9/22/2022
then two different tables for likes and dislikes
Message Not Public

Sign In and Join Server To See

9/22/2022
Message Not Public

Sign In and Join Server To See

9/22/2022
Lary
Lary9/22/2022
yes but that's not api. It's my controller and its models
Lary
Lary9/22/2022
there are more columns in vote
Message Not Public

Sign In and Join Server To See

9/22/2022
Message Not Public

Sign In and Join Server To See

9/22/2022
Lary
Lary9/22/2022
how do i do that in sql database?
Lary
Lary9/22/2022
another column perhaps?
Message Not Public

Sign In and Join Server To See

9/22/2022
Lary
Lary9/22/2022
no i mean the calculation. How do I do that in database?
Lary
Lary9/22/2022
I thought you meant calculating and writing it in an another column
Message Not Public

Sign In and Join Server To See

9/22/2022
Lary
Lary9/22/2022
in a moment
Lary
Lary9/22/2022
it froze lol, a minute more
Lary
Lary9/22/2022
Message Not Public

Sign In and Join Server To See

9/22/2022
Lary
Lary9/22/2022
yup
Message Not Public

Sign In and Join Server To See

9/22/2022
Lary
Lary9/22/2022
do you mean api?
Message Not Public

Sign In and Join Server To See

9/22/2022
Message Not Public

Sign In and Join Server To See

9/22/2022
Lary
Lary9/22/2022
finally, i was trying to understand why you was talking about database calculation
Message Not Public

Sign In and Join Server To See

9/22/2022
Message Not Public

Sign In and Join Server To See

9/22/2022
Lary
Lary9/22/2022
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();
Lary
Lary9/22/2022
i guess i have to add that code into the dto
Message Not Public

Sign In and Join Server To See

9/22/2022
Message Not Public

Sign In and Join Server To See

9/22/2022
Lary
Lary9/22/2022
wait a min I am confused 😄
Lary
Lary9/22/2022
will postdto have a custom property for the karma value?
Lary
Lary9/22/2022
do I calculate it in constructer or something?
Message Not Public

Sign In and Join Server To See

9/22/2022
Message Not Public

Sign In and Join Server To See

9/22/2022
Message Not Public

Sign In and Join Server To See

9/22/2022
Message Not Public

Sign In and Join Server To See

9/22/2022
Lary
Lary9/22/2022
i am not sure if i want to use linq or not 😄
Message Not Public

Sign In and Join Server To See

9/22/2022
Lary
Lary9/22/2022
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 😄
Lary
Lary9/22/2022
anyway, i am gonna eat something, i will be back in 20 min if you want to say something, thanks for the help!
Message Not Public

Sign In and Join Server To See

9/22/2022
Message Not Public

Sign In and Join Server To See

9/22/2022
Message Not Public

Sign In and Join Server To See

9/22/2022
Lary
Lary9/22/2022
I use body for the main focus on tables. Body can be the text in comment for example
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy