creating a linq to sql model from a generic list

Posted on 2014-07-11
Last Modified: 2016-02-15
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
Question by:maqskywalker
    LVL 77

    Expert Comment

    by:David Johnson, CD, MVP
    Have you considered using the views that are associated with AdventureWorks?
    LVL 83

    Accepted Solution

    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

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now