Crazy Linq: replacing multiple and nested foreach statements with a query

Objective of the method: determine whether the given EnvDTE code class contains the given GeneratedCodeAttribute:

"old" foreach/if approach:

private bool IsPresentationModel(CodeClass2 baseClass)
{
    foreach (CodeClass2 pc in baseClass.PartialClasses)
    {
        foreach (CodeAttribute2 attr in pc.Attributes)
        {
            if (attr.FullName == typeof(GeneratedCodeAttribute).FullName &&
                attr.Value.Contains(ThisAssembly.Title))
            {
                return true;
            }
        }
    }

    foreach (CodeAttribute2 attr in baseClass.Attributes)
    {
        if (attr.FullName == typeof(GeneratedCodeAttribute).FullName &&
            attr.Value.Contains(ThisAssembly.Title))
        {
            return true;
        }
    }

    return false;
}
...

Read full article

No Comments