Last active
          August 29, 2015 14:01 
        
      - 
      
- 
        Save hagbarddenstore/67ec5ae007764a6b50ae to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | void Main() | |
| { | |
| var root = new FamilyItem | |
| { | |
| Name = "A", | |
| FamilyItems = new List<FamilyItem> | |
| { | |
| new FamilyItem | |
| { | |
| Name = "B", | |
| FamilyItems = new List<FamilyItem> | |
| { | |
| new FamilyItem(), | |
| new FamilyItem(), | |
| new FamilyItem() | |
| } | |
| }, | |
| new FamilyItem | |
| { | |
| Name = "C", | |
| FamilyItems = new List<FamilyItem> | |
| { | |
| new FamilyItem(), | |
| new FamilyItem(), | |
| new FamilyItem(), | |
| new FamilyItem() | |
| } | |
| } | |
| } | |
| }; | |
| Func<FamilyItem, IEnumerable<FamilyItem>> flatten = null; | |
| flatten = (FamilyItem x) => | |
| { | |
| return x.FamilyItems.SelectMany(p => flatten(p)) | |
| .Concat(new[] { x }); | |
| }; | |
| var largestFamily = from family in flatten(root) | |
| let count = family.FamilyItems != null ? family.FamilyItems.Count : 0 | |
| orderby count descending | |
| select family; | |
| Console.WriteLine(largestFamily.First().Name); | |
| } | |
| class FamilyItem | |
| { | |
| public FamilyItem() | |
| { | |
| FamilyItems = new List<FamilyItem>(); | |
| } | |
| public string Name { get; set; } | |
| public List<FamilyItem> FamilyItems { get; set; } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment