1

我有一个 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} 

在此处输入图像描述

4

1 回答 1

1

事实上,您可以这样重写您的查询:

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

然后你可以导出结果:

导出 NDepend 查询结果

于 2021-07-15T07:43:05.727 回答