LINQ - SELECT NEW - 包含ListBox所选项

时间:2014-01-30 14:25:44

标签: asp.net vb.net linq

使用ASP.NET 4.0

我在LINQ上遇到麻烦,无法将.srvc_id作为选中的值,我的查询只填充了我的对象的第一个复选框。

代码

If Not IsNothing(lbServices.SelectedItem) Then
            'NEW record INSERT values from list box to table.
            myServices = (From item In lbServices.Items
                                    Where item.Selected
                                    Select New tbl_parent_to_child With
                                            {
                                                    .id = myServices.child_id,

                                                    ----- .srvc_id is where I am stuck any help would be great.  It is repeating the first selected check box.

                                                    .srvc_id = lbServices.SelectedValue,
                                                    .Active = True,
                                                    .CreateDate = DateTime.Now,
                                                    .CreateByID = SecurityMethods.GetLoginUserId(Session("AuthUser")),
                                                    .UpdateDate = DateTime.Now,
                                                    .UpdateByID = SecurityMethods.GetLoginUserId(Session("AuthUser"))
                                            }).ToList()
            For Each item In myServices 
                ctx.tbl_parent_to_child .Add(item)
            Next
        End If

Try
            ctx.SaveChanges()
        Catch ex As Exception
            Me.Master.HandleStatusMessageEvent((New StatusMessageEventArgs("Email a screen shot of this error to the Help Desk", StatusMessageType.ErrorMsg)))
        End Try

1 个答案:

答案 0 :(得分:1)

我相信你想使用项目变量.srvc_id = CInt(item.Value)

这类似于Listbox in asp.net not getting selected items