• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

creating a linq to sql model from a generic list

I have a ASP.NET MVC application.
I'm currently using the Employees table of the Northwind Sql Server table.

Example 1:  a generic list of objects for the Employees table

I created a generic list of object for this table which I call like this. I use this as my Model in my MVC application.:

Dim employees As List(Of Employees)

The code for my Employees class looks like this:

    Public Class Employees

        Private m_EmployeeID As Int64
        Private m_LastName As String
        Private m_FirstName As String

        Public Property EmployeeID() As Int64
                Return m_EmployeeID
            End Get
            Set(value As Int64)
                m_EmployeeID = value
            End Set
        End Property
        Private m_EmployeeID As Int64
        Public Property LastName() As String
                Return m_LastName
            End Get
            Set(value As String)
                m_LastName = value
            End Set
        End Property
        Private m_LastName As String

        Public Property FirstName() As String
                Return m_FirstName
            End Get
            Set(ByVal value As String)
                m_FirstName = value
            End Set
        End Property

    End Class

Open in new window

The connection class I use called Connection looks like this:

    Public Class Connection
        Public Function mydata() As DataSet
            Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ToString())
            Dim cmd As New SqlCommand("select * from Employees", con)
            cmd.CommandType = CommandType.Text
            Dim da As New SqlDataAdapter()
            da.SelectCommand = cmd
            Dim myrec As New DataSet()
            Return myrec
        End Function

    End Class

Open in new window

I know I can use the Object Relational Designer to create a Linq to Sql model for my Employees table called Employees.dbml.

But I was wondering.... If I already have a list of objects created in code just like in Example 1 above.

Can I create a Linq to Sql model from those objects (or from that generic list which is called Employees) ?

See right now in one of my Views I was using a Linq to Sql data model of the employees table which I had called Employees.dbml but now instead of using that linq model I want to use a generic list model of my Employees table.

When I was using linq I was querying that table like this:
If you notice, Visual Studio underlined db.Employees  which is a datatable that was in Employees.dbml but I deleted this model. I want to use the business objects I created in example 1.

I guess to add to my question, can I create a DataTable called Employees from my generic list so I can use in my linq query like this and the bold item below is the name of my DataTable. If so, how would I create DataTable from my business objects or generic list?

Dim q = From c In db.Employees
            Select c
1 Solution
David Johnson, CD, MVPOwnerCommented:
Have you considered using the views that are associated with AdventureWorks?
To create data table from your list, you will need to loop through the list and add rows to data table which are populated from the object properties. If you want to make it generic, you can use reflection to loop through all properties of an object and add those to the table.

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now