NDepend:如何从查询中导出结果

NDepend: how to export the result from a query

我有一个 CQLinq 查询,其中 returns 一个方法列表和每个方法的成员列表。导出查询结果只会显示元素个数。我考虑过使用 Linq Aggregate( (a,b) => a + ',' + b)。有更好的解决方案吗?

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} 

确实可以这样重写查询:

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"
} 
  • 方法和字段都考虑在内
  • 使用字段的方法和方法使用的成员聚合在一个字符串中

然后就可以导出结果了: