VB.net LINQ [英] VB.net LINQ

查看:127
本文介绍了VB.net LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始创建一个点击事件,希望在其中
1.匹配员工的名字和姓氏以及(员工ID),
2.如果该员工已经存在,则在页面上返回一条消息,
3.我也想在文本框中显示下一个可用的员工ID.
用户希望显示ID.

我花了很多时间试图正确编写代码以正确显示ID和消息.以下是语法上正确的部分:

如果您能正确编写此书,可以给我任何帮助,我将不胜感激.

I began creating a click event in which I hope to
1. match the employee first and last name and (employee ID),
2. return a message to the page if the employee already exists,
3. I also want to display the next available employee ID in a textbox.
The user wants the ID to show.

I spent many hours trying to correctly write the code to display the ID and messages appropriately. The following is the portion that is syntactically correct:

I would appreciate any help you can give me in writing this correctly.

Protected Sub btnUpdate2_Click(ByVal sender As Object, ByVal e As EventArgs) 
        Handles btnUpdate2.Click
        Using context As New personnelDataContext()
            Dim employees = From c In context.Employees _
                Where (c.lastName.Contains(txtLastName.Text.Trim())
                 AndAlso c.firstName.Contains(txtFirstName.Text.Trim())) _
                Select c

            If (employees Is Nothing) Then
                'there was no matching employee in the database
                Dim employee As New Employee()
                context.Employees.InsertOnSubmit(employee)
                context.SubmitChanges()
            End If
        End Using
    End Sub

推荐答案

您遇到什么困难?

And what are you having difficulty with?

if(employee == null)
  // add new 
  // display employee.ID
else
  // display message


如果数据库中没有匹配的员工,则employees变量将不是Nothing.相反,它将是一个空集合.

更改
The employees variable will not be Nothing if there was no matching employee in the database. Rather, it will be an empty collection.

Change
If (employees Is Nothing) Then
    'there was no matching employee in the database
    Dim employee As New Employee()
    context.Employees.InsertOnSubmit(employee)
    context.SubmitChanges()
End If




to

If (employees Is Nothing OrElse employees.Count() < 1) Then
    'there was no matching employee in the database
    Dim employee As New Employee()
    context.Employees.InsertOnSubmit(employee)
    context.SubmitChanges()
End If



您还应该检查查询中使用的字符串比较(lastName.Contains()firstName.Contains())是否区分大小写.我相信他们现在是.

要获得更多帮助,您需要确切说明遇到的问题.



You should also check whether or not the string comparisons (lastName.Contains() and firstName.Contains()) you use in your query should be case-sensitive. Right now I believe they are.

For additional help, you''ll need to clarify exactly what problems you''re having.


非常感谢您的答复.我在显示下一个可用的employee_id是文本框的语法上遇到麻烦.

我写了以employee_id作为输出的存储过程.单击更新按钮时,只想在文本框中显示它即可.
Thank you very much for the responses. I''m having trouble with the syntax of displaying the next available employee_id is a text box.

I wrote the stored procedure with employee_id as an output. Just want to show it in the text box when the update button is clicked.


这篇关于VB.net LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆