I have the following linq statement which works great, when each gameServer
in the list has a collection of connectedClients
.
but when the connectedClient
is null
, the query crashes.
How can I prevent this from crashing?
var connectedClients = (from x in gameServers
from y in x.ConnectedClients
select new
{
x.Name,
x.GameType,
ConnectedClients = new
{
y.ClientName,
y.ConnectedOn,
y.ClientIpAddressAndPort
}
}).ToList();
and..
public class GameServer
{
public int Id;
public ICollection<Client> ConnectedClients;
...
}