Beginner question:
Currently (for testing purposes) this game has two playing pieces with the following names:
public enum JapaneseUnit
{
None,
JapaneseDesDivOne,//playing piece
JapaneseCruDivOne,//playing piece
NotAllowed,
}
The two playing pieces are in the following example (the string I’m looking for is "JapaneseDesDivOneJapaneseCruDivOne")
string side2ForceType;
if (Side2Army > -1)
{
var side2Army = campaign.Armies[Side2Army];
var japaneseArmiesInArea = campaign.ActiveArmies().Where(x => x.Location ==
side2Army.Location && side2Army.Realm == 0).ToList();
//List of all Japanese armies in area (in this case it is two)
for (var unitKey = 0; unitKey < japaneseArmiesInArea.Count(); unitKey++)
//count == 2
{
side2ForceType = japaneseArmiesInArea[unitKey].JapaneseUnit.ToString();
//Output is "None", I'm looking for "JapaneseDesDivOneJapaneseCruDivOne"
}
}
In the future there will be many more playing pieces so while the following is technically correct and closer to what I’m looking for it will be unworkable. I’d like to exclude the "None" Output which is why I’d like to use a loop:
side2ForceType = japaneseArmiesInArea[0].JapaneseUnit.ToString() +
japaneseArmiesInArea[1].JapaneseUnit +
japaneseArmiesInArea[2].JapaneseUnit +
japaneseArmiesInArea[3].JapaneseUnit + etc etc;
//Output is JapaneseDesDivOneJapaneseCruDivOneNoneNone
I’m sure I’m missing something basic.
>Solution :
Would something like this achieve what you are trying to do?
string.Join("", japaneseArmiesInArea.Where(a => a.JapaneseUnit != JapaneseUnit.None)
.Select(a => a.JapaneseUnit.ToString()));
This is assuming that any of your items in japaneseArmiesInArea actually do have the value of JapaneseUnit set to something other than None. If not, you have a problem somewhere else.