将动态生成的对象放入数组(或集合)中?
我为自定义对象创建了一个自定义类。我像这样实例化:
Dim class_info as New class_level
然后我使用 LINQ-to-Entities 查询来提取一些信息:
Dim get_config = From p in dbContext.Configs _
Where p.Type = "classdate" _
Select p
然后我有一个 For..Each 循环,我想根据这些信息创建对象的实例,如下所示:
For Each row In get_config
Dim the_row = row
class_info.class_level = the_row.Description
class_info.dateo = the_row.dateValue
class_info.timeo = the_row.timeValue
class_info.datec = the_row.dateValue
class_info.timec = the_row.timeValue
class_info.query = "q" + the_row.Description
Next
现在,我有两个问题。首先,现在这只是一次又一次地覆盖相同的对象属性。我需要以某种方式动态命名该对象。然后,我需要将这些对象放入一个数组中,以便我可以迭代它们......
I created a custom class for a custom object. I instantiate like so:
Dim class_info as New class_level
Then I use a LINQ-to-Entities query to pull some information:
Dim get_config = From p in dbContext.Configs _
Where p.Type = "classdate" _
Select p
Then I have a For..Each loop that I want to create an instance of the object from this information like so:
For Each row In get_config
Dim the_row = row
class_info.class_level = the_row.Description
class_info.dateo = the_row.dateValue
class_info.timeo = the_row.timeValue
class_info.datec = the_row.dateValue
class_info.timec = the_row.timeValue
class_info.query = "q" + the_row.Description
Next
Now, I have two problems. First, right now this is just overwriting the same object properties, again, and again. I need to somehow dynamically name the object. Then, I need to place these objects in an array so I can iterate through them...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您真正应该做的是将查询直接投影到类型中:
然后您最终会得到
class_level
的IEnumerable
,我认为这就是您想要的?您应该考虑更好地命名事物,因为很难理解您在这里使用的内容。
What you really should do is to project your query into the type directly:
Then you will end up with an
IEnumerable
ofclass_level
, which I think is what you want?You should consider naming things a bit better as it's hard to understand what you are working with here.