2

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;
    ...
}
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
  • 2
    `where y != null`? It seems too easy, I must have wrongly understood your question. – jv42 Nov 17 '11 at 10:22
  • I don't see a variable called `connectedClient`. Am I missing something? – Polynomial Nov 17 '11 at 10:22
  • 1
    Personally I would change your code such that `ConnectedClients` is **empty** rather than `null` when there are no connected clients (see eg [this](http://stackoverflow.com/questions/1969993/is-it-better-to-return-null-or-empty-collection) ). – AakashM Nov 17 '11 at 10:43

3 Answers3

8

If it is null, use a value that isn't null instead:

var connectedClients = (
    from x in gameServers
    from y in x.ConnectedClients ?? Enumerable.Empty<Client>()
    // ...

The ?? is called the null-coalescing operator.

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
7

add a where to check for null before second from

var connectedClients = (from x in gameServers
                        where x.ConnectedClients != null
                        from y in x.ConnectedClients
                        select new
                        {
                            x.Name,
                            x.GameType,
                            ConnectedClients = new
                            {
                                y.ClientName,
                                y.ConnectedOn,
                                y.ClientIpAddressAndPort
                            }
                        }).ToList();
Muhammad Hasan Khan
  • 34,648
  • 16
  • 88
  • 131
0
IEnumerable<GameServer> gameServesWIthConnectedClients = from x in gameServers 
                       where x.ConnectedClients != null  
                       select x;

var connectedClients = from y in gameServesWIthConnectedClients
                       select
                           new
                               {
                                   y.Name,
                                   y.GameType,
                                   ConnectedClients =
                           new
                               {
                                   y.ConnectedClients.ClientName,
                                   y.ConnectedClients.ConnectedOn,
                                   y.ConnectedClients.ClientIpAddressAndPort
                               }
                               };
connectedClients = connectedClients.ToList();
Matt Evans
  • 7,113
  • 7
  • 32
  • 64