Skip to content

Instantly share code, notes, and snippets.

@vdovinanton
Last active June 7, 2017 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdovinanton/b75ef947a78b028a114d3c8bf63d3ce7 to your computer and use it in GitHub Desktop.
Save vdovinanton/b75ef947a78b028a114d3c8bf63d3ce7 to your computer and use it in GitHub Desktop.
Gets all child items
private static IEnumerable<CollectionMemberCached> Descendants(CollectionMemberCached root)
{
var nodes = new Stack<CollectionMemberCached>(new[] { root });
while (nodes.Any())
{
CollectionMemberCached node = nodes.Pop();
yield return node;
foreach (var n in node.Members)
nodes.Push(n);
}
}
//usage
var collecationMembers =
allCollections.SelectMany(q => q.Members)
.SelectMany(Descendants)
.Where(q => q.Type == CollectionMemberType.User && !q.Excluded)
.ToList();
/*
public class CollectionMemberCached
{
public CollectionMemberCached()
{
Members = new List<CollectionMemberCached>();
}
public Guid ObjectGuid { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public bool Excluded { get; set; }
public CollectionMemberType Type { get; set; }
public List<CollectionMemberCached> Members { get; set; }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment