2

我需要以下答案的 vb.net 语法(在 c# 中给出)-

如何在不迭代的情况下将 Linq 结果转换为 DTO 类对象

我曾尝试在此链接上转换为 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
4

2 回答 2

1

好吧,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
于 2013-07-24T08:20:55.083 回答
0

它现在工作了 - 以下是转换器中缺少的关键字。

  1. 命令
  2. 选择新建
  3. 删除关键字 Key

最终代码如下 -

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
于 2013-07-24T08:25:51.057 回答