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
            Get
                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
            Get
                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
            Get
                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()
            da.Fill(myrec)
            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:
linq.png
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
LVL 1
maqskywalkerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
Have you considered using the views that are associated with AdventureWorks?
0
CodeCruiserCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.