首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将模型添加到列表

问无法将模型添加到列表
EN

Stack Overflow用户
提问于 2012-11-09 15:20:16
回答 1查看 241关注 0票数 0

我执行以下for each循环将模型插入到列表中:

代码语言:javascript
复制
        Dim weights = weight.Where(Function(x) x.FW_Food_ID = item.NDB_No)

        For Each i In weights
            Dim model = New Tbl_Food_Weight
            model.FW_GmWt = i.FW_GmWt
            Debug.Print("i.FW_GmWt=" + i.FW_GmWt)
            Debug.Print("model.FW_GmWt=" + model.FW_GmWt)
            model.FW_Food_ID = i.FW_Food_ID
            Debug.Print("i.FW_Food_ID=" + i.FW_Food_ID)
            Debug.Print("model.FW_Food_ID=" + model.FW_Food_ID)
            model.FW_Option = i.FW_Option
            Debug.Print("i.FW_Option=" + i.FW_Option)
            Debug.Print("model.FW_Option=" + model.FW_Option)
            model.FW_ID = i.FW_ID
            Debug.Print("i.FW_ID=" + i.FW_ID.ToString)
            Debug.Print("model.FW_ID=" + model.FW_ID.ToString)
            item.test.Add(model)
        Next

我的模型如下:

代码语言:javascript
复制
Public Class Tbl_Food_Weight

    <Key()> _
    Public Property FW_ID As Integer
    Public Property FW_Food_ID As String
    Public Property FW_Option As String
    Public Property FW_Serving_Size As String
    Public Property FW_GmWt As String

End Class

我的test属性如下:

代码语言:javascript
复制
<NotMapped()> _
Public Overridable Property test As List(Of Tbl_Food_Weight)

我得到的输出是:

代码语言:javascript
复制
i.FW_GmWt=250
model.FW_GmWt=250
i.FW_Food_ID=12695
model.FW_Food_ID=12695
i.FW_Option=1
model.FW_Option=1
i.FW_ID=4481
model.FW_ID=4481
A first chance exception of type 'System.NullReferenceException' occurred in MyBlog.DLL

此错误发生在这一行:

代码语言:javascript
复制
item.test.Add(model)

你看,它放进了变量,但是当我把模型添加到列表中时,它说它是空的。如何将模型添加到模型列表中?

谢谢。

编辑:

背景项目:

代码语言:javascript
复制
For Each item In food

            Dim weights = weight.Where(Function(x) x.FW_Food_ID = item.NDB_No)
            ... rest of code already posted above
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-09 15:23:19

模型不是空的--您的测试属性是。在任何项的构造函数中,都需要“新建”集合。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13310749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档