0

次の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

私のモデルは次のとおりです。

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財産は次のとおりです。

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

私が得る出力は次のとおりです。

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

エラーは次の行で発生します。

item.test.Add(model)

ご覧のとおり、変数が挿入されますが、モデルをリストに追加しようとすると、null と表示されます。モデルをモデルのリストに追加するにはどうすればよいですか?

ありがとうございました。

編集:

コンテキスト内のアイテム:

For Each item In food

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

1 に答える 1

3

モデルは null ではありません -- テスト プロパティは null です。どんなアイテムのコンストラクターでも、コレクションを「新しく」する必要があります。

于 2012-11-09T15:23:19.190 に答える