请有人帮我从以下两种方法中用 lambda 替换 foreach,否则将不胜感激。
/// <summary>
/// This method will find and remove the Template entries that has no person entris
/// from List<Template> Templates
/// </summary>
/// <param name="Persons"></param>
/// <param name="Templates"></param>
public bool ClearOrphnedIDs(List<Person> Persons, List<Template> Templates)
{
bool isClearComplete = false;
try
{
if (Persons != null && Templates != null)
{
List<string> OrphnedTemplatesNeedToIgnore = new List<string>();
foreach (Template template in Templates)
{
string personID = Persons.Find(p => p.PersonID == template.PersonID).PersonID;
if (string.IsNullOrEmpty(personID) && !OrphnedTemplatesNeedToIgnore.Contains(personID))
{
OrphnedTemplatesNeedToIgnore.Add(template.PersonID);
DataSyncLog.Warn(string.Format("Templates with personID {0} is orphned (has no person entry) in DB", template.PersonID));
}
}
if (OrphnedTemplatesNeedToIgnore.Count > 0)
Templates.RemoveAll(t=> OrphnedTemplatesNeedToIgnore.Contains(t.PersonID));
isClearComplete = true;
}
}
catch (Exception ex)
{
DataSyncLog.Debug(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType + "::" + System.Reflection.MethodBase.GetCurrentMethod().ToString() + " :: " + ex.Message + " :: " + ex.StackTrace);
}
return isClearComplete;
}
/// <summary>
/// This method will Find and remove the Person and template entries that has
/// zero or odd number of templates. from List<Person> Persons and List<Template> Templates
/// </summary>
/// <param name="Persons"></param>
/// <param name="Templates"></param>
public bool ClearInconsistantIDs(List<Person> Persons, List<Template> Templates)
{
bool isClearComplete = false;
try
{
if (Persons != null && Templates != null)
{
List<string> personNeedtoIgnoreAlongWithItsTemplates = new List<string>();
foreach (Person person in Persons)
{
int templatesCount = Templates.FindAll(t => t.PersonID == person.PersonID).Count;
if (templatesCount == 0 || templatesCount % 2 != 0)
{
personNeedtoIgnoreAlongWithItsTemplates.Add(person.PersonID);
if (templatesCount == 0)
DataSyncLog.Warn(string.Format("Person with Registration No: {0} and personID {1} has no Templates in DB. Templates Count: {2}", person.RegistrationNO, person.PersonID, templatesCount));
else
DataSyncLog.Warn(string.Format("Person with Registration No: {0} and personID {1} has inconsistent data (Templates) in DB. Templates Count: {2}", person.RegistrationNO, person.PersonID, templatesCount));
}
}
if (personNeedtoIgnoreAlongWithItsTemplates.Count > 0)
{
Templates.RemoveAll(t => personNeedtoIgnoreAlongWithItsTemplates.Contains(t.PersonID));
Persons.RemoveAll(p => personNeedtoIgnoreAlongWithItsTemplates.Contains(p.PersonID));
}
isClearComplete = true;
}
}
catch (Exception ex)
{
DataSyncLog.Debug(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType + "::" + System.Reflection.MethodBase.GetCurrentMethod().ToString() + " :: " + ex.Message + " :: " + ex.StackTrace);
}
return isClearComplete;
}