MikeInvise
❔ Using Linq to Add or Update Score in Highscore List Object
Hi,
I created a Highscore object, which consists of an IntValue and StringValue objects. I am trying to use Linq to add or update a highscore to the list. The list contains a name and the corresponding score. I want to be able to project a name and if the name exists, select it's IntValue and add to it the score, otherwise, if the projected name does not exist within the object, I want to add it. I was thinking of something like highscores.Where(x => x.StringValue.Equals("Player).AddOrUpdate(y=>y.IntValue+=thisScore));
I thought maybe I would need to create my own IEnumberable AddOrUpdate, and I tried to make something, but then I got really stuck. At this point I really don't know what I am doing, but I really want to understand and know how to do this. I would very much prefer not to use Fors and ForEachs within this code, because I have multiple if statements in a block that are running inside a for loop, and they are one lines. If I have to do long hand code, I'm going to have to copy and paste that 8 times and make it really messy and complicated. Thank you in advance for the help!
10 replies