linqndependcqlinq

NDepend: how to export the result from a query


I've got a CQLinq query which returns a list of methods and a list of members for each of them. Exporting the query result will only show the number of elements. I thought about using a Linq Aggregate( (a,b) => a + ',' + b). Is there a better solution?

let type = Application.Types.WithFullName("WPF.ViewModels.CouponViewModel").Single()
let dicoFields  =  type.Fields
   .ToDictionary(f => f, f => f.MethodsUsingMe.Where(m => m.ParentType == f.ParentType))
let dicoMethods = type.Methods
   .ToDictionary(m => m, m => m.MembersUsed.Where(f => f.ParentType == m.ParentType))

// The partition algorithm on both dicos here

//from pair in dicoFields 
//orderby   pair.Value.Count() descending
//select new { pair.Key, pair.Value } 

from pair in dicoMethods 
orderby   pair.Value.Count() descending
select new { pair.Key, pair.Value} 

enter image description here


Solution

  • Indeed you can rewrite your query this way:

    let type = Application.Types.WithFullName("WPF.ViewModels.CouponViewModel").Single()
    let dicoMembers  =  type.ChildMembers
       .ToDictionary(x => x, x => 
      x.IsField ? x.AsField.MethodsUsingMe.Where(m => m.ParentType == x.ParentType):
                  x.AsMethod.MembersUsed.Where(f => f.ParentType == x.ParentType))
    
    from pair in dicoMembers
    orderby   pair.Value.Count() descending
    select new { 
     pair.Key, 
     str = pair.Value.Any() ?
        pair.Value.Select(x => x.Name).Aggregate( (a,b) => a + " ; " + b) :
        "empty"
    } 
    

    Then you can export the result:

    Exporting NDepend query result