c.im is one of the many independent Mastodon servers you can use to participate in the fediverse.
C.IM is a general, mainly English-speaking Mastodon instance.

Server stats:

2.9K
active users

#csharp

89 posts58 participants0 posts today

#linq #csharp

以下方法可以安全地从一个可能为空的集合中获取最大值,并在集合为空时返回一个默认值。代码如下:

```csharp
var maxValue = aList.Select(a => a.aIntField).DefaultIfEmpty(0).Max();
```

### 解释:
- `Select(a => a.aIntField)`: 选择每个对象的 `aIntField` 字段。
- `DefaultIfEmpty(0)`: 如果 `aList` 为空,返回一个包含 `0` 的集合。
- `Max()`: 计算最大值。

这样,如果 `aList` 为空,`maxValue` 将会是 `0`,避免了抛出异常的风险。