Easy to do with LINQ:
if (mixedDeviceList.Count == 1)
{
return string.Format("Device {0} does not belong to this lot. It belongs to lot {1}.", mixedDeviceList[0].ID, mixedDeviceList[0].AssignedLot);
}
string firstLot = mixedDeviceList[0].AssignedLot;
bool multipleLots = mixedDeviceList.Skip(1).Any(d => d.AssignedLot != firstLot);
if (multipleLots)
{
return string.Format("There are {0} devices from multiple lots mixed with the current lot.", mixedDeviceList.Count);
}
return string.Format("There are {0} devices from lot [{1}] mixed with the current lot.", mixedDeviceList.Count, firstLot);
Or, if you'd prefer to avoid LINQ:
if (mixedDeviceList.Count == 1)
{
return string.Format("Device {0} does not belong to this lot. It belongs to lot {1}.", mixedDeviceList[0].ID, mixedDeviceList[0].AssignedLot);
}
string firstLot = mixedDeviceList[0].AssignedLot;
bool multipleLots = false;
for (int index = 1; index < mixedDeviceList.Count; index++)
{
if (mixedDeviceList[index].AssignedLot != firstLot)
{
multipleLots = true;
break;
}
}
if (multipleLots)
{
return string.Format("There are {0} devices from multiple lots mixed with the current lot.", mixedDeviceList.Count);
}
return string.Format("There are {0} devices from lot [{1}] mixed with the current lot.", mixedDeviceList.Count, firstLot);