Reputation: 109
I am new to Linq-to-SQL, so forgive me if this is a dumb question, but what is the best way to cast a LINQ-to-SQL result set to a list of a specific class?
For example, let's say I have a class defined called clsTest. It has three properties, Prop1, Prop2 and Prop3 (all strings). What I need to do is produce a list(of clsTest) from a linq-to-sql result set where I am selecting the same fields that are contained in my class.
For example, if I get my results like this:
Dim i as new MyDataContext
Dim results = From o in i.SomeTable
Select o.Field1, o.Field2, o.Field3
How would I get results to be a list of clsTest? Or is there a way to do it in the initial linq query? Something Like:
Dim i as new MyDataContext
Dim results as list(of clsTest) = From o in i.SomeTable
Select o.Field1, o.Field2, o.Field3
Thanks in advance!
Upvotes: 0
Views: 64
Reputation: 109
Shortly after posting, I found the answer I was looking for:
Dim i as new MyDataContext
Dim results as IQueryable(Of clstest) = From o in i.SomeTable
Select New clsTest With {.Prop1 = o.Field1, .Prop2 = o.Field2, o.Prop3 = o.Field3}
Upvotes: 0
Reputation: 478
try using object contractor and set it up internally. results = From o in i.SomeTable Select New clsTest( o.Field1, o.Field2, o.Field3)
Upvotes: 1