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

问题描述

我有一个 CQLinq 查询,它返回一个方法列表和每个方法的成员列表。导出查询结果只会显示元素数量。我想过使用 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} 

enter image description here

解决方法

确实可以这样重写查询:

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

然后就可以导出结果了:

Exporting NDepend query result

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...