在linq查询中,我需要以某种方式将对linq查询期间正在创建的对象的引用传递给在同一查询中创建的另一个类...我知道这个问题并不清楚,所以请看下面的例子。
public class class1
public class class2
private _class1 as class1
public property OtherProperty1 as int
Public sub new(cls1 as class1)
_class1 = cls1
End Sub
End class
public property Property1 as int
public property Property2 as class2
End Class
Dim e as List(of class1) = (From row as datarow in dataTable.Rows
Select New class1 with {
.Property1 = row.Item("Property1"),
.Property2 = new class2(<<< ME >>>) with {
.OtherProperty1 = row.Item("OtherProperty1")
}
}).ToList()
所以,在上面的代码中,&#34;&lt;&lt;&lt; ME&gt;&gt;&gt;&#34;找到了,我需要传递给新的class2对象一个对正在创建的class1对象的引用。
我知道可以使用的一个替代方法是在此查询期间创建class1对象,跳过对class2对象的引用。然后,稍后,在所有class1对象上使用foreach循环,并使用新的linq查询来填充缺少的class2对象......它将起作用,但它并不理想。我想立刻填充所有内容,而不是一次完成部分内容。
- EDIT-- 理想情况下,我不必更改现有的类,因为它可能会在代码的其他部分产生不必要的副作用...如果我必须进行更改,我会愿意做一些我正在创建的新的构造函数或属性,如某些答案所示。
有什么想法吗?
答案 0 :(得分:0)
因为Class2的构造函数需要Class1的实例,你能不能创建它?
Dim myClass1 As New class1
Dim e as List(of class1) = (From row as datarow in dataTable.Rows
Select New class1 with {
.Property1 = row.Item("Property1"),
.Property2 = new class2(myClass1) with {
.OtherProperty1 = row.Item("OtherProperty1")
}
}).ToList()
答案 1 :(得分:0)
您可以使用构造函数:
Class class1
Public Property Property1 As Object
Public Property Property2 As class2
Public Sub New(Property1 As Object, OtherProperty1 As Object)
Me.Property1 = Property1
Property2 = new class2(Me, OtherProperty1)
End Sub
End Class
Class class2
Private otherClass As class1
Public Property OtherProperty1 As Object
Public Sub New(class1 As class1, OtherProperty1 As Object)
otherClass = class1
Me.OtherProperty1 = OtherProperty1
End Sub
End Class
Dim e as List(of class1) = (From row as datarow in dataTable.Rows
Select New class1(
row.Item("Property1"),
row.Item("OtherProperty1")
)).ToList()
答案 2 :(得分:0)
好的,我不是 VB 的专家,这可能会做得更好,但你可以这样做:
Dim e As List(Of Class1) = (From row As DataRow In dataTable.Rows
Select Create(row)).ToList()
Function Create(row As DataRow) As Class1
Dim result As Class1 = New Class1 With
{.Property1 = row.Item("Property1")}
result.Property2 = New Class2(result) With
{.OtherProperty1 = row.Item("OtherProperty1")}
Return result
End Function
我试图在没有额外功能的情况下做到这一点,但如果可以在 VB 中进行,我就不会这样做。
答案 3 :(得分:0)
我做过类似的事情。在class1
Public Class class1
Public ReadOnly Property this As class1
Get
Return Me
End Get
End Property
End Class
然后你可以拨打.this
Dim e as List(of class1) = (From row as datarow in dataTable.Rows
Select New class1 with {
.Property1 = row.Item("Property1"),
.Property2 = new class2(.this) with {
.OtherProperty1 = row.Item("OtherProperty1")
}
}).ToList()
我没有测试过这个
答案 4 :(得分:0)
据我所知,在实例化对象时,您无法引用对象的实例,因为该对象尚不存在。 即你不能这样做:
Public Class foo
Public Sub New(bar As foo)
End Sub
End Class
Dim bar As New foo(bar) ' Null Reference Exception
问题中的源代码不是一个完整的示例,因为您正在class1
中访问未定义的属性,如果不在class2
下对其进行限定,则无法引用class1
。因此,我已经采取了一些自由来将缺少的声明添加到您的代码中。我在class2上添加了.toString()
函数,只是为了表明正在引用_class1
。请注意我在下面的代码中使用VB 14;并非本示例的核心需要它,但使用$""
比String.Format()
更清洁
以下是我的代码版本:
Public Class class1
Public Property Property1 As Object
Public Property Property2 As class2
Public Class class2
Private _class1 As class1
Public Property OtherProperty1 As Int32
Public Sub New(cls1 As class1)
_class1 = cls1
End Sub
Public Property Property1 As Int32
Public Overrides Function toString() As String
Return $"{_class1.Property1.ToString} success"
End Function
End Class
End Class
这是我的解决方案:
Dim dt As New DataTable()
dt.Columns.AddRange({New DataColumn("Property1"), New DataColumn("OtherProperty1")})
dt.Rows.Add({"foobar", 123I})
Dim CreateNewClass1 = Function(x As DataRow)
Dim cls1 As New class1 With {.Property1 = x.Item("Property1")}
cls1.Property2 = New class1.class2(cls1) With {
.OtherProperty1 = x.Item("OtherProperty1")}
Return cls1
End Function
Dim e As List(Of class1) = (From row As DataRow In dt.Rows
Select CreateNewClass1(row)).ToList()
e.ForEach(Sub(x) Console.WriteLine($"{x.ToString}
class1.property1 = {x.Property1.ToString}
class1.Property2 = {x.Property2.toString}
class2.otherProperty1 = {x.Property2.OtherProperty1}"))
Console.ReadLine()