c#检查IEnumerable是否为null

时间:2016-12-07 00:50:32

标签: c# ienumerable

我正在尝试将IEnuemrable<SystemUser>的类型检查为null。

我正在使用这段代码

            var systemUsers = newActActivityPersons.SelectMany(x => x.Person.SystemUsers);
            if (systemUsers == null || !systemUsers.Any()) return ;

这不起作用。当我尝试执行systemUsers.ToArray()systemUsers.ToList()之类的操作时,我会得到一个null异常。如何查看错误?

1 个答案:

答案 0 :(得分:0)

后续x.Person.SystemUsers之一可能为空。如果有.Any(),则返回true。如果您之后尝试ToList(),则可能会发现x Personx.Person为空。

检查以确保while(1){ listen(sockfd,5); // determine the size of a clientAddressInfo struct clilen = sizeof(clientAddressInfo); int *newsockfd = malloc(sizeof(int)); // block until a client connects, when it does, create a client socket *newsockfd = accept(sockfd, (struct sockaddr *) &clientAddressInfo, &clilen); // if the connection blew up for some reason, complain and exit if (*newsockfd < 0){ error("ERROR on accept"); } connection_args *args = malloc(sizeof(connection_args)); args->file_descrp = newsockfd; pthread_t tid; pthread_create(&tid,NULL, handle_connect, args); } void * handle_connect(void* args){ connection_args* connect_arg = (connection_args*)args; pthread_detach(pthread_self()); int n = -1; char buffer[256]; bzero(buffer,256); //while not close; while(1){ // try to read from the client socket n = read(*connect_arg->file_descrp,buffer,255); printf("input: %s\n", buffer); // if the read from the client blew up, complain and exit if (n < 0){ error("ERROR reading from socket"); } int fd; if(strcmp("open",buffer) == 0){ fd = open("file.txt",0); bzero(buffer,256); sprintf(buffer,"%d",fd); }else if(strcmp("read",buffer) == 0){ char *read_buffer = malloc(sizeof(char)*256); bzero(read_buffer,256); fd = read(get_filedescrp(),read_buffer,30); bzero(buffer,256); sprintf(buffer,"%s,%d",read_buffer,fd); }else if(strcmp("close",buffer) == 0){ break; } printf("buffer_send: %s\n",buffer); // try to write to the client socket n = write(*connect_arg->file_descrp,buffer,sizeof(buffer)); // if the write to the client below up, complain and exit if (n < 0){ printf("here!!\n"); error("ERROR writing to socket"); } bzero(buffer,256); } printf("Left thread\n"); return NULL; } 在访问对象上的属性之前不为空。