将 C# Linq 查询转换为 VB.Net Linq
本文关键字:Linq VB Net 查询 转换 | 更新日期: 2023-09-27 18:37:24
我有一个项目,我在 if 条件中使用 Linq 查询来检查条件是真还是假,并且它在我的 C# 项目中工作正常。现在我有另一个项目,我想使用相同的东西,但问题是这个项目 vb.net,我在转换 Linq 查询时遇到问题。我在 c# 中的 linq 查询是这样的:
#region Data Members
static List<UserDetail> ConnectedUsers = new List<UserDetail>();
static List<MessageDetail> CurrentMessage = new List<MessageDetail>();
#endregion
#region Methods
public void Connect(string userName)
{
var id = Context.ConnectionId;
if (ConnectedUsers.Count(x => x.ConnectionId == id) == 0)
{
ConnectedUsers.Add(new UserDetail { ConnectionId = id, UserName = userName });
// send to caller
Clients.Caller.onConnected(id, userName, ConnectedUsers, CurrentMessage);
// send to all except caller client
Clients.AllExcept(id).onNewUserConnected(id, userName);
}
}
}
它的 vb.net 等价物是这样的:
#Region "Data Members"
Shared ConnectedUsers As New List(Of UserDetail)()
Shared CurrentMessage As New List(Of MessageDetail)()
#End Region
#Region "Methods"
Public Sub Connect(userName As String)
Dim id = Context.ConnectionId
If ConnectedUsers.Count(Function(x) x.ConnectionId = id) = 0 Then
ConnectedUsers.Add(New UserDetail() With { _
Key .ConnectionId = id, _
Key .UserName = userName _
})
' send to caller
Clients.Caller.onConnected(id, userName, ConnectedUsers, CurrentMessage)
' send to all except caller client
Clients.AllExcept(id).onNewUserConnected(id, userName)
End If
End Sub
错误屏幕截图是这样的
我得到了您需要做这样的事情的解决方案:
如果 ConnectedUsers.AsEnumerable.Count(Function(x) x.ConnectionId = id) = 0,则