0

foreach を以下の 2 つのメソッドのラムダに置き換えるのを手伝ってくれる人もいれば、それ以外の最適化を歓迎します。

/// <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;
}
4

3 に答える 3

4

ラムダを使用するとうまくいくとは思いませんが、HashSet<string>代わりにパラメーターの値を使用List<string>して別HashSet<string>PersonID値を作成することで、より良い結果が得られることは間違いありません。Persons

public bool ClearOrphnedIDs(List<Person> Persons, List<Template> Templates)
{
    bool isClearComplete = false;
    try
    {
        if (Persons != null && Templates != null)
        {
            var personsSet = new HashSet<string>(Persons.Select(p => p.PersonId));
            var orphnedTemplatesNeedToIgnore = new HastSet<string>();
            foreach (var template in Templates)
            {
                if (!personsSet.Contains(template.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;
}
于 2013-09-06T07:10:39.243 に答える
1

拡張メソッドを定義しますExceptBy

var result = Templates.ExceptBy(Persons, t => t.PersonID, p => p.PersonID)
                      .ToList();

public static IEnumerable<TSource> ExceptBy<TSource, TKey, TDest>(
        this IEnumerable<TSource> first,
        IEnumerable<TDest> second,
        Func<TSource, TKey> keySelector1,
        Func<TDest, TKey> keySelector2)
{
    HashSet<TKey> keys = new HashSet<TKey>(second.Select(keySelector2));

    return first.Where(k => keys.Add(keySelector1(k)));
}
于 2013-09-06T08:46:36.337 に答える