使用LINQ查找相同的属性值字段

时间:2013-07-28 11:21:13

标签: vb.net visual-studio-2010 linq

我的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),我需要过滤/查找具有相同脚本配置的字段具有相同的脚本名称,具有相同的输入参数配置(Name,Type,FieldName,FixValue以及计数器配置(StartValue,StepValue))。

如何使用LINQ查询或通过EqualityComparer或任何其他方式实现此目的?

示例

==================

以下是示例函数。 GetFields()函数返回List(of Field),我需要List(Of Field),它具有相同的ScriptObject配置(相同的属性值)。在下面的GetFields()函数中,FirstField和SecondField在脚本名称,InputParam属性方面具有相同的ScriptObject。所以,我希望IGrouping(of String,List(Of Field))(ScriptName('Script1')作为字符串,List(Of Field)具有来自List(Of Field)的第一和第二字段项目,使用LINQ或任何方式返回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不可能具有不同的输入参数计数和参数名称。只有参数类型可以不同。

感谢。

0 个答案:

没有答案