I have a List item
List xmlValue = new List();
In this I have Item {\"English\",\"Spanish\",\"French\",\
List<string> xmlValue = new List<string>()
{"English", "Spanish", "French", "Hindi", "English", "English"};
string search = "English";
int[] result = xmlValue.Select((b, i) => b.Equals(search) ? i : -1)
.Where(i => i != -1).ToArray();
I'd opt against using the LINQ extension methods in this case and use an "old-fashioned" loop:
string search = "English";
var foundIndices = new List<int>(xmlValue.Count);
for (int i = 0; i < xmlValue.Count; i++) {
if (xmlValue[i] == search) {
foundIndices.Add(i);
}
}
It's simply more readable like this in my opinion; also, the foundIndices
list never holds any unwanted values.