问题描述
我有一个 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}
解决方法
确实可以这样重写查询:
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"
}
- 同时考虑方法和字段
- 使用字段的方法和方法使用的成员聚合在一个字符串中
然后就可以导出结果了: