在VB.NET中检索Linq to SQL关系数据

时间:2014-10-16 08:52:23

标签: sql vb.net linq relational-database

我需要通过表检索关系数据:scan_LogEntry,我知道如何通过运行多个linq-sql查询来检索所有这些数据,但我很确定这不是一种非常有效的方法。我当前正在运行此语句以从scan_LogEntry中检索一大块数据。但我想显示所有名称而不是所有主键。我一直在阅读如何做到这一点,但我发现的解释对我来说太复杂了。目前正在提出这样的数据。

Public Class form_LogEntry_2
Private STleanappscontext As New STleanappsDataContext
Private scanbindsource As New BindingSource
Private Sub form_LogEntry_2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim begdt As DateTime = New DateTime(2014, 8, 4)
    Dim enddt As DateTime = New DateTime(2014, 8, 5)

    Dim logentryquery = From scan_LogEntries In STleanappscontext.scan_LogEntries
                        Where scan_LogEntries.tstStart >= begdt And scan_LogEntries.tstStart <= enddt
                        Select scan_LogEntries

    DataGridView1.DataSource = logentryquery
    DataGridView1.AutoResizeColumns()
End Sub
End Class

scan_LogEntries表通过以下列(userCode,projectCode,actionCode,departmentCode)与4个其他表保持多对一关系有一种简单的方法可以检索这些数据而无需在列表中循环4次并查找单独列出每个主键?

1 个答案:

答案 0 :(得分:1)

使用Join语句链接其他表

Dim logentryquery = From scan_LogEntries In STleanappscontext.scan_LogEntries
                            Join user In Usertable
                            On user.id Equals scan_LogEntries.usercode
                        Where scan_LogEntries.tstStart >= begdt And scan_LogEntries.tstStart <= enddt
                        Select scan_LogEntries, user.name