2

I need vb.net syntax of below answer(given in c#)-

How can I convert Linq results to DTO class object without iteration

I have tried converting to vb.net at this link, but it does give compilation errors.

Following is the output from above convertor -

Public Function [Get]() As List(Of User)
Using db = New MyContext()
    Return (From u In db.UsersOrder By u.FirstNameNew User() With { _
        Key .Id = u.pkUser, _
        Key .Username = u.Username, _
        Key .Password = u.Password, _
        Key .Active = u.Active _
    }).ToList()
End Using
End Function
Community
  • 1
  • 1
Chris_web
  • 743
  • 3
  • 10
  • 19

2 Answers2

1

Well, the Linq query is obviously messed up. And since there's no User class in the code you tried to convert, the converter tried to use the Key keyword, which is only used for anonymous types.

So the correct code should look like:

Public Function [Get]() As List(Of User)
    Using db = New MyContext()
        Return (From u In db.Users
                Order By u.FirstName
                Select New User() With { 
                    .Id = u.pkUser, 
                    .Username = u.Username, 
                    .Password = u.Password, 
                    .Active = u.Active 
                }).ToList()
    End Using
End Function

assuming the following User class:

Public Class User
    Public Id As Integer
    Public Username As String
    Public Password As String
    Public Active As Boolean
End Class
sloth
  • 99,095
  • 21
  • 171
  • 219
0

It worked now - Following are the keyword were missing in convertor.

  1. Order
  2. Select New
  3. removed keyword Key

and final code is as below -

Public Function [Get]() As List(Of User)
Using db = New MyContext()
Return (From u In db.UsersOrder Order By u.FirstNameNew select new User() With { _
    .Id = u.pkUser, _
    .Username = u.Username, _
    .Password = u.Password, _
    .Active = u.Active _
}).ToList()
End Using
Chris_web
  • 743
  • 3
  • 10
  • 19