简体   繁体   English

无法将模型添加到列表中

[英]Can't Add Model to List

I perform the following for each loop to insert models into a list: for each循环执行以下操作以将模型插入到列表中:

        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

My model is as follows: 我的模型如下:

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

My test property is as follows: 我的test属性如下:

<NotMapped()> _
Public Overridable Property test As List(Of Tbl_Food_Weight)

The output that I get is: 我得到的输出是:

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

The error occurs on this line: 此行发生错误:

item.test.Add(model)

You see, it puts in the variables, but when I go to add the model to the list, it says it's null. 你看,它放入了变量,但是当我把模型添加到列表中时,它说它是空的。 How can I add the model to the list of models? 如何将模型添加到模型列表中?

Thank you. 谢谢。

Edit: 编辑:

Item in context: 上下文中的项目:

For Each item In food

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

model isn't null -- your test property is. model不为null - 您的测试属性是。 In the constructor of whatever item is, you need to "new" your collection. 在任何项目的构造函数中,您需要“新”您的集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM