【发布时间】:2019-05-29 04:37:44
【问题描述】:
我有一个 linq 查询,它通过匹配方法名称将两个表连接在一起。在表中,我需要查找“全局”值,如果找到,则将称为“参数”的方法中的子元素相互比较,以确保两个表具有相同的对应项,并且如果一个方法参数与其他任何一个参数不匹配然后返回假。
以下是结果示例。我们可以看到 seqEquals 找到了一个不等于其他任何方法的“方法”,但是,LINQ 查询返回所有值,我想只提取“seqEquals”的假我尝试将字符串比较放在其中'where' 子句,但是结果并没有像预期的那样相互比较。
{ mOneName = GetItemsJSON, seqEqual = False, mOneParm = [
{
"name": "itemTypes",
"type": "List"
},
{
"name": "textSearch",
"type": "String"
}
], mTwoParm = [
{
"name": "propertyId",
"type": "String"
},
{
"name": "revClassIds",
"type": "List"
},
{
"name": "itemTypes",
"type": "List"
},
{
"name": "textSearch",
"type": "String"
},
{
"name": "eventId",
"type": "String"
}
] }
C# 代码
var ST1 = firstOrgST["records"][0]["SymbolTable"]["methods"];
var ST2 = secondOrgST["records"][0]["SymbolTable"]["methods"];
// joins both tables and pulls the methods containing global from the joins
var STDiff =
from one in ST1
join two in ST2
on one.Value<String>("name") equals two.Value<string>("name")
where one["modifiers"].Values().Contains("global")
select new
{
mOneName = one["name"],
seqEqual = one["parameters"].ToString() == two["parameters"].ToString(),
mOneParm = one["parameters"],
mTwoParm = two["parameters"]
};
var errorFound = false;
var errorMethod = "";
foreach (var i in STDiff)
{ Console.WriteLine(i);
if (!i.seqEqual)
{
errorFound = true;
errorMethod = i.mOneName.ToString();
break;
}
}
// if error is found prints to console the class name as well as the method name
if (errorFound)
{
Console.WriteLine(Path.GetFileNameWithoutExtension(symbolTable) + " on method: " + errorMethod + " has different parameters.\n");
isDifferent = true;
}
数据示例:这是表一。表二可能有不同的参数
{
"SymbolTable": {
"methods": [
{
"annotations": [
{
"name": "TestVisible"
}
],
"location": {
"column": 20,
"line": 1056
},
"modifiers": [
"private"
],
"name": "GetItemsJSON",
"parameters": [
{
"name": "propertyId",
"type": "String"
},
{
"name": "revClassIds",
"type": "List"
},
{
"name": "itemTypes",
"type": "List"
},
{
"name": "itemCategories",
"type": "List"
},
{
"name": "textSearch",
"type": "String"
},
{
"name": "eventId",
"type": "String"
}
],
"references": [],
"returnType": "String",
"type": null
},
{
"annotations": [],
"location": {
"column": 26,
"line": 4313
},
"modifiers": [
"static",
"global"
],
"name": "GetItemsJSON",
"parameters": [
{
"name": "itemTypes",
"type": "List"
},
{
"name": "textSearch",
"type": "String"
}
],
"references": [],
"returnType": "String",
"type": null
},
{
"annotations": [],
"location": {
"column": 26,
"line": 4316
},
"modifiers": [
"static",
"global"
],
"name": "GetItemsJSON",
"parameters": [
{
"name": "propertyId",
"type": "String"
},
{
"name": "revClassIds",
"type": "List"
},
{
"name": "itemTypes",
"type": "List"
},
{
"name": "textSearch",
"type": "String"
},
{
"name": "eventId",
"type": "String"
}
]
}
]
}
}
【问题讨论】:
-
您能否添加更多详细信息以显示您的表格数据是什么样的以及您期望的输出是什么?
-
输出的是我发布的json。我真正需要的是返回的方法名称。这些表有超过 25k 行代码,我可以尽我所能尽可能缩短它。
-
where one["modifiers"].Values().Contains("global") && one["parameters"].ToString() != two["parameters"].ToString()工作吗? -
没有。它不会比较所有参数,因为每个参数都有多个。
-
为了理解为什么参数检查不起作用,了解它是什么数据类型很重要。你能发布一些示例数据吗 - 4,5 行就可以了。