c# - 如何在 CQLinq 中进行分组?
问题描述
我有一个自动生成的接口和实现,它定义了 3000 多个方法,并为每个方法定义了各自的异步签名。总共有 6,000 多种方法。
我想弄清楚哪些没有使用。这是我到目前为止的 CQLinq:
// Unused methods in the interface
let notUsed1 = new HashSet<IMethod>(
from t in JustMyCode.Types
where t.Name == "IDataProcessor"
from m in t.Methods
where !m.HasAttribute("xyz.CoreService.CoreServiceOperationAttribute") && !m.MethodsCallingMe.Any()
select m)
// Unused methods in the concrete implementation of the interface
let notUsed2 =
from t in JustMyCode.Types
where t.Name == "DataProcessor"
from m in t.Methods
where m.HasAttribute("System.CodeDom.Compiler.GeneratedCodeAttribute") && !m.MethodsCallingMe.Any()
// Obtain the methods in the intersection
select m
from m in notUsed2 where notUsed1.Contains(m.OverriddensBase.Single())
let baseName = m.SimpleName.Replace("Async", "")
group m by baseName into g
select new { g }
唉,这行不通。错误信息是:
Ln 18 Col 8 Type {IGrouping`2<String,IMethod>} not accepted to type first result argument.
Only IMethod, IField, IType, INamespace, IAssembly, IMember, ICodeElement, ICodeElementParent, ICodeContainer, IIssue and IRule are accepted to type first result argument.
看来group
是无计可施了。我的想法是按基本名称(同步和异步签名相同)对方法进行分组,并选择那些具有 2 个项目的条目。但看起来我的计划行不通,因为不支持分组。
你会怎么做?
解决方案
我设法通过将分组键更改为IMethod
对象来解决它:
// Collect the sync methods
let syncMethods = (
from t in JustMyCode.Types
where t.Name == "IDataProcessor"
from m in t.Methods
where !m.HasAttribute("xyz.CoreService.CoreServiceOperationAttribute") && !m.IsAsync
select m).ToDictionary(m => m.SimpleName)
// Unused methods in the interface
let notUsed1 = new HashSet<IMethod>(
from t in JustMyCode.Types
where t.Name == "IDataProcessor"
from m in t.Methods
where !m.HasAttribute("xyz.CoreService.CoreServiceOperationAttribute") && !m.MethodsCallingMe.Any()
select m)
// Unused methods in the concrete implementation of the interface
let notUsed2 =
from t in JustMyCode.Types
where t.Name == "DataProcessor"
from m in t.Methods
where m.HasAttribute("System.CodeDom.Compiler.GeneratedCodeAttribute") && !m.MethodsCallingMe.Any()
// Select methods in the intersection
select m
from m in notUsed2 where notUsed1.Contains(m.OverriddensBase.Single())
let syncMethod = syncMethods[m.SimpleName.Replace("Async", "")]
group m by syncMethod into g // group by the respective sync method
where g.Count() == 2 // return if both sync and async signatures are not used
select new { g.Key }
我想知道如何简化。
推荐阅读
- javascript - 将多级列表扩展到 2 级和 3 级
- python - 如何在没有 Flask 或 Django 的情况下处理基本 HTML 身份验证
- reactjs - React+to 获取包含名称、类型和二进制数据的文件详细信息,以及所有足以让后端系统存储图像的详细信息
- java - Java Spring Boot,线程未关闭
- c - 如何通过 C 编程打开终端中显示的链接?
- c# - MySql不会连接到datagrid wpf
- sql-server - 强制 SQL Server 关闭 DML 查询的结果集
- javascript - 在 gatsby 中使用不同的模板创建页面
- java - 如何将生产者类外部的值添加到生产者线程(阻塞队列实现)
- c++ - 为什么我的构造函数没有像它应该的那样初始化它的超类属性?