我需要以下答案的vb.net语法(在c#中给出) -
How can I convert Linq results to DTO class object without iteration
我已尝试在this link转换为vb.net,但确实会出现编译错误。
以下是上述转换器的输出 -
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
答案 0 :(得分:0)
嗯,Linq查询显然搞砸了。由于您尝试转换的代码中没有User
类,因此转换器尝试使用Key
关键字,该关键字仅用于匿名类型。
所以正确的代码应如下所示:
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
假设以下User
类:
Public Class User
Public Id As Integer
Public Username As String
Public Password As String
Public Active As Boolean
End Class
答案 1 :(得分:0)
现在有效 - 以下是转换器中缺少的关键字。
,最终代码如下 -
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