我的 VB.NET 项目中有以下课程。
Public Class Field
Public Property FieldName As String
Public Property ScriptObject As Script
End Class
Public Class Script
Public Property ScriptName As String
Public Property InputParamList As List (Of InputParam)
Public Property OutputParam As String
End Class
Public Class InputParam
Public Property ParamName As String
Public Property Type As String //Type can be Field, FixValue or Counter
Public Property FieldName As String
Public Property FixValue As String
Public Property CounterObject As Counter
End Class
Public Class Counter
Public Property StartValue As Integer
Public Property StepValue As Integer
End Class
我的一项业务功能返回给我List(Of Field)
。从中List(Of Field)
,我需要过滤/查找具有相同脚本配置的字段意味着具有相同的脚本名称,具有相同的输入参数配置(名称,类型,字段名称,FixValue,以及计数器配置(StartValue,StepValue))。
如何使用 LINQ 查询或通过 EqualityComparer 或任何其他方式实现此目的?
例子
===================
以下是示例函数。函数从我需要的具有相同配置(相同属性值)的GetFields()
函数返回。在下面的函数中,FirstField 和 SecondField 在脚本名称、InputParam 属性方面具有相同的 ScriptObject。因此,我希望(ScriptName ('Script1') as string 和 List(Of Field) 具有第一个和第二个字段项)通过使用 LINQ 或任何其他方式从返回中。List(of Field)
List(Of Field)
ScriptObject
GetFields()
IGrouping(of String,List(Of Field))
List(Of Field)
GetFields()
Public Function GetFields() As List(Of Field)
Dim oFieldList As New List(Of Field)
Dim oFirstScript As Script = GetFirstScript()
Dim oSecondScript As Script = GetSecondScript()
Dim oFirstField As New Field
oFirstField.Name = "FirstField"
oFirstField.ScriptObject = New Script()
oFirstField.ScriptObject.Name = oFirstScript.Name
oFirstField.ScriptObject.InputParamList = new List(Of InputParam)
For Each oInParam In oFirstScript.InputParamList
Dim oInputParam = New InputParam()
oInputParam.Name = oInParam.Name
oInputParam.Type = oInParam.Type
oInputParam.FixVaue = oInParam.FixValue
oInputParam.CounterObject = oInParam.CounterObject
oFirstField.ScriptObject.InputParamList.Add(oInputParam)
Next
oFirstField.OutputParam = oFirstScript.OutputParam
oFieldList.Add(oFirstField)
//Second Field
Dim oSecondField As New Field
oSecondField.Name = "SecondField"
oSecondField.ScriptObject = New Script()
oSecondField.ScriptObject.Name = oFirstScript.Name
oSecondField.ScriptObject.InputParamList = new List(Of InputParam)
For Each oInParam In oFirstScript.InputParamList
Dim oInputParam = New InputParam()
oInputParam.Name = oInParam.Name
oInputParam.Type = oInParam.Type
oInputParam.FixVaue = oInParam.FixValue
oInputParam.CounterObject = oInParam.CounterObject
oSecondField.ScriptObject.InputParamList.Add(oInputParam)
Next
oSecondField.OutputParam = oFirstScript.OutputParam
oFieldList.Add(oSecondField)
//Third Field
Dim oThirdField As New Field
oThirdField.Name = "ThirdField"
oThirdField.ScriptObject = New Script()
oThirdField.ScriptObject.Name = oSecondScript.Name
oThirdField.ScriptObject.InputParamList = new List(Of InputParam)
For Each oInParam In oSecondScript.InputParamList
Dim oInputParam = New InputParam()
oInputParam.Name = oInParam.Name
oInputParam.Type = oInParam.Type
oInputParam.FixVaue = oInParam.FixValue
oInputParam.CounterObject = oInParam.CounterObject
oThirdField.ScriptObject.InputParamList.Add(oInputParam)
Next
oThirdField.OutputParam = oSecondScript.OutputParam
oFieldList.Add(oThirdField)
Return oFieldList
End Function
Public Function GetFirstScript() As Script
Dim oScript As New Script
oScript.Name = "Script1"
oScript.InputParamList = New List(Of InputParam)
Dim oFieldInputParam As New InputParam
oFieldInputParam.Name = "Param1"
oFieldInputParam.Type = "Field"
oFieldInputParam.FieldName = "FirstField"
oFieldInputParam.FixValue = String.Empty
oFieldInputParam.CounterObject = Nothing
oScript.InputParamList.Add(oFieldInputParam)
Dim oFixInputParam As New InputParam
oFixInputParam.Name = "Param2"
oFixInputParam.Type = "FixValue"
oFixInputParam.FieldName = String.Empty
oFixInputParam.FixValue = "123"
oFixInputParam.CounterObject = Nothing
oScript.InputParamList.Add(oFixInputParam)
Dim oCounterInputParam As New InputParam
oCounterInputParam.Name = "Param3"
oCounterInputParam.Type = "Counter"
oCounterInputParam.FieldName = String.Empty
oCounterInputParam.FixValue = String.Empty
oCounterInputParam.CounterObject = New Counter
oCounterInputParam.CounterObject.StartValue = 1
oCounterInputParam.CounterObject.StepValue = 1
oScript.InputParamList.Add(oCounterInputParam)
oScript.OutputParam = "returnResult"
Return oScript
End Function
Public Function GetSecondScript() As Script
Dim oScript As New Script
oScript.Name = "Script2"
oScript.InputParamList = New List(Of InputParam)
Dim oFieldInputParam As New InputParam
oFieldInputParam.Name = "Param1"
oFieldInputParam.Type = "Field"
oFieldInputParam.FieldName = "SecondField"
oFieldInputParam.FixValue = String.Empty
oFieldInputParam.CounterObject = Nothing
oScript.InputParamList.Add(oFieldInputParam)
Dim oFixInputParam As New InputParam
oFixInputParam.Name = "Param2"
oFixInputParam.Type = "FixValue"
oFixInputParam.FieldName = String.Empty
oFixInputParam.FixValue = "XYZ"
oFixInputParam.CounterObject = Nothing
oScript.InputParamList.Add(oFixInputParam)
Dim oCounterInputParam As New InputParam
oCounterInputParam.Name = "Param3"
oCounterInputParam.Type = "Counter"
oCounterInputParam.FieldName = String.Empty
oCounterInputParam.FixValue = String.Empty
oCounterInputParam.CounterObject = New Counter
oCounterInputParam.CounterObject.StartValue = 1
oCounterInputParam.CounterObject.StepValue = 1
oScript.InputParamList.Add(oCounterInputParam)
oScript.OutputParam = "returnResult"
Return oScript
End Function
注意:ScriptName 是唯一的。因此,相同的脚本名称 ScriptObject 不可能有不同的输入参数计数和参数名称。只有参数类型可以不同。
谢谢。