0

I need to set 7 items from a match to 7 strings, so instead of doing something like this

var item0 = items.Data.Where(p => p.Key.Equals(matches[i].Participants[participantNum].stats.Item0.ToString())).FirstOrDefault().Key;
var item1 = items.Data.Where(p => p.Key.Equals(matches[i].Participants[participantNum].stats.Item1.ToString())).FirstOrDefault().Key;
var item2 = items.Data.Where(p => p.Key.Equals(matches[i].Participants[participantNum].stats.Item2.ToString())).FirstOrDefault().Key;
var item3 = items.Data.Where(p => p.Key.Equals(matches[i].Participants[participantNum].stats.Item3.ToString())).FirstOrDefault().Key;
//...

I thought of doing something easier in a for loop like this

string[] itemsList = new string[7];
for (int j = 0; j < itemsList.Length; j++)
    {
        string nextItem = $"Item{j}";
        itemsList[j] = items.Data.Where(p => p.Key.Equals(matches[i].Participants[participantNum].stats.nextItem.ToString())).FirstOrDefault().Key; //problem is here
    }

But c# doesn't realize I'm trying to use the nextItem string. Is there a way I can use a string to define what item I want?

NotUnique
  • 13
  • 1
  • 2
    Dictionaries are your friend. – ProgrammingLlama Apr 05 '20 at 13:09
  • did you try using var nextItem = $"Item{j}"? – Mosia Thabo Apr 05 '20 at 13:10
  • @Mosia What effect would that have? It will still be compiled as `string nextItem = ` – ProgrammingLlama Apr 05 '20 at 13:11
  • It's also not clear where you are trying to use your nextItem within the loop because the nextItem visible on that Linq seems to be a property of another object. – Mosia Thabo Apr 05 '20 at 13:12
  • @Mosia OP means that they have a class with properties like `Item0`, `Item1`, etc. and they want to access those properties by a string. – ProgrammingLlama Apr 05 '20 at 13:13
  • I understand that, I am just not seeing that part you're explaining on the code that's is why I am asking. – Mosia Thabo Apr 05 '20 at 13:14
  • My Opinion: I wouldn't declare a variable for that unless I am going to use it more than once.. What I would do is use _$"Item{j}"_ exactly where I want to use it to avoid extra work. – Mosia Thabo Apr 05 '20 at 13:16
  • @NotUnique What is the type of "stats" ( in Participants[participantNum].stats ) ? – Christian Kouamé Apr 05 '20 at 13:16
  • OP: The short answer is that this isn't possible as a feature of the language. One workaround is [reflection](https://stackoverflow.com/questions/5508050/how-to-get-a-property-value-based-on-the-name/5508090), but that can be slow and it's better to architect your code around accessing the data you need, rather than retroactively hacking access to it. As such, I'd suggest something like a dictionary, depending on your scenario. – ProgrammingLlama Apr 05 '20 at 13:21
  • @John how should I use the dictionary in this situation (in your opinion)? – NotUnique Apr 05 '20 at 13:28
  • @ChristianKouamé stats contain variables of the players, in this case though I only take the selected one ``` public int Item0 { get; set; } public int Item1 { get; set; } public int Item2 { get; set; } public int Item3 { get; set; } public int Item4 { get; set; } public int Item5 { get; set; } public int Item6 { get; set; } //trinket? public int Kills { get; set; } public int Deaths { get; set; } public int Assists { get; set; } //... ``` – NotUnique Apr 05 '20 at 13:30
  • 4
    Whenever you start creating variables or properties with numbers at the end like that is a good indication that you should use some type of collection instead. – juharr Apr 05 '20 at 13:30

1 Answers1

1

You can use reflection. ie:

string[] itemsList = new string[7];
var stats = matches[i].Participants[participantNum].stats;
for (int j = 0; j < itemsList.Length; j++)
{
    var nextItem = stats.GetType().GetProperty($"item{j}").GetValue(stats).ToString();
    var v = items.Data.Where(p => p.Key.Equals(nextItem)).FirstOrDefault(); //problem is here
    itemsList[j] = (v != null)?v.Key:"";
}

PS: Probably in your model, you should define an List Items for an easier manipulation.

Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39
  • A list of items is a problem, I get the data with json text and can't convert it that well. But thanks for the help! – NotUnique Apr 05 '20 at 15:28