LINQ select null values from List

I have a List<string> and it may contain null values in random indexes. I want to check which elements null and select that elements for throwing message.

What i am using;

List<string> getNames = EbaUsers.GetNamesFromIds(activeDirectoryInfo[7],
 activeDirectoryInfo[8], activeDirectoryInfo[9], activeDirectoryInfo[10]);

if(getNames[7].Equals(null))
{
     MessageBox.Show("getNames[7] is null");
}
if(getNames [8].Equals(null))
{
     MessageBox.Show("getNames[8] is null");
}
if(getNames[9].Equals(null))
{
     MessageBox.Show("getNames[9] is null");
}
if(getNames[10].Equals(null))
{
     MessageBox.Show("getNames[10] is null");
}

I know this is very easy to do with LINQ but i haven't found anywhere.

Thanks for your help.

Jon Skeet
people
quotationmark

Sounds like you want to first project the strings to index/value pairs, then pick the elements with null values, and project to just the indexes:

var nullIndexes = names.Select((value, index) => new { value, index })
                       .Where(pair => pair.value == null)
                       .Select(pair => pair.index)
                       .ToList();

people

See more on this question at Stackoverflow