我有一个类collection
的列表Collection1
,想要获得名称“N1”的 Pin 列表,按名称进行初始选择,我应该得到那些以“.1”或(“。2”结尾)的引脚然后在列表中找到另一个连接“.2”或(“。1”)
结果应该是与此类似的列表
R1.1
NoUse1.10
L1.2
R1.2
NoUse1.19
C1.2
C1.1
NoUse2.3
L1.1
NoUse2.11
我只是得到这个
R1.1
NoUse1.10
L1.2
R1.2
L1.1
有没有办法在LINQ中以编程方式更新JOIN-ON? 谢谢
这是我的代码
Public Class Form1
Private Property connCompsNetsSel As Object
Private Property Result As IEnumerable(Of String)
Private Property Result2 As IEnumerable(Of String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim collection As New List(Of Collection1)
collection.Add(New Collection1("N1", "R1.1"))
collection.Add(New Collection1("N1", "NoUse1.10"))
collection.Add(New Collection1("N1", "L1.2"))
collection.Add(New Collection1("N2", "R1.2"))
collection.Add(New Collection1("N2", "NoUse1.19"))
collection.Add(New Collection1("N2", "C1.2"))
collection.Add(New Collection1("N3", "C1.1"))
collection.Add(New Collection1("N3", "NoUse2.3"))
collection.Add(New Collection1("N4", "NoUse2.6"))
collection.Add(New Collection1("N4", "C2.2"))
collection.Add(New Collection1("N5", "C2.1"))
collection.Add(New Collection1("N5", "C3.1"))
collection.Add(New Collection1("N6", "C7.2"))
collection.Add(New Collection1("N6", "C7.2"))
collection.Add(New Collection1("N10", "L1.1"))
collection.Add(New Collection1("N10", "NoUse2.11"))
'get initial pins from given name
Result = From cns In collection _
Where cns.Name = "N1" Select cns.Pin
'get other pins
Result2 = From cns In collection _
Join cns1 In collection On _
If(cns.Pin.ToString.Split(".")(1) = "1", cns.Pin.ToString.Split(".")(0) & ".2", cns.Pin.ToString.Split(".")(0) & ".1") Equals cns1.Pin.ToString _
Where cns.Name = "N1" Select cns1.Pin
'combine Result and Result2
Dim finalResult As New ArrayList
finalResult.AddRange(Result.ToArray)
finalResult.AddRange(Result2.ToArray)
End Sub
End Class
Public Class Collection1
Private _Name As String
Private _Pin As String
Public Sub New(ByVal Name As String, ByVal Pin As String)
_Name = Name
_Pin = Pin
End Sub
Public Property Name As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Public Property Pin As String
Get
Return _Pin
End Get
Set(ByVal Value As String)
_Pin = Value
End Set
End Property
End Class
答案 0 :(得分:0)
public result As List(Of Collection1)
public tempresult As List(Of Collection1)
Sub Main
Dim collection As New List(Of Collection1)
collection.Add(New Collection1("N1", "R1.1"))
collection.Add(New Collection1("N1", "NoUse1.10"))
collection.Add(New Collection1("N1", "L1.2"))
collection.Add(New Collection1("N2", "R1.2"))
collection.Add(New Collection1("N2", "NoUse1.19"))
collection.Add(New Collection1("N2", "C1.2"))
collection.Add(New Collection1("N3", "C1.1"))
collection.Add(New Collection1("N3", "NoUse2.3"))
collection.Add(New Collection1("N4", "NoUse2.6"))
collection.Add(New Collection1("N4", "C2.2"))
collection.Add(New Collection1("N5", "C2.1"))
collection.Add(New Collection1("N5", "C3.1"))
collection.Add(New Collection1("N6", "C7.2"))
collection.Add(New Collection1("N6", "C7.2"))
collection.Add(New Collection1("N10", "L1.1"))
collection.Add(New Collection1("N10", "NoUse2.11"))
'get all pins
result=new List(Of Collection1)()
tempresult = collection.Where(Function(c) c.Name="N1").ToList()
While(tempresult.Any())
result.AddRange(tempresult)
tempresult= _
(from r In result _
from cns In collection _
join cns1 In collection _
on cns.Name equals cns1.Name _
where r.PinMain = cns.PinMain _
AND (r.PinSub="1" OR r.PinSub="2") _
AND NOT (result.Contains(cns1)) _
select cns1).ToList()
End While
Dim finalResult=result.Select(Function(r) r.Pin)
finalResult.Dump()
End Sub
Public Class Collection1
Private _Name As String
Private _Pin As String
Public Sub New(ByVal Name As String, ByVal Pin As String)
_Name = Name
_Pin = Pin
End Sub
Public Property Name As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Public Property Pin As String
Get
Return _Pin
End Get
Set(ByVal Value As String)
_Pin = Value
End Set
End Property
Public ReadOnly Property PinMain as String
Get
Return _Pin.Split(".")(0)
End Get
end property
public ReadOnly Property PinSub as String
Get
Return _Pin.Split(".")(1)
end Get
End Property
End Class
' Define other methods and classes here
给出输出:
R1.1
NoUse1.10
L1.2
R1.2
NoUse1.19
C1.2
L1.1
NoUse2.11
C1.1
NoUse2.3