将动态生成的对象放入数组(或集合)中?

发布于 2024-10-31 11:32:13 字数 774 浏览 0 评论 0原文

我为自定义对象创建了一个自定义类。我像这样实例化:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

成熟稳重的好男人 2024-11-07 11:32:13

您真正应该做的是将查询直接投影到类型中:

Dim class_infos = From p in dbContext.Configs _
                  Where p.Type = "classdate" _
                  Select New class_level With {
                     .Prop = p.Prop
                     ' other properties here
                  }

然后您最终会得到 class_levelIEnumerable,我认为这就是您想要的?

您应该考虑更好地命名事物,因为很难理解您在这里使用的内容。

What you really should do is to project your query into the type directly:

Dim class_infos = From p in dbContext.Configs _
                  Where p.Type = "classdate" _
                  Select New class_level With {
                     .Prop = p.Prop
                     ' other properties here
                  }

Then you will end up with an IEnumerable of class_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文