【发布时间】:2019-08-20 13:55:20
【问题描述】:
我有一个代码:
var status = ...
var StatusMapping = new Dictionary<AnotherStatus, IList<Status>>
{
{
AnotherStatus,
new List<Status>
{
Status1,
Status2
}
}
}
foreach (var keyValuePair in StatusMapping)
{
if (keyValuePair.Value.Contains(status))
{
return keyValuePair.Key;
}
}
throw Exception();
我是 C# 新手,从 Java 切换过来的。在java中很容易做到这一点:
return StatusMapping
.entrySet()
.stream()
.filter(e -> e.getValue().contains(status))
.map(Map.Entry::getKey)
.findFirst()
.orElseThrow(() -> new Exception());
有没有办法用 LINQ 做到这一点?
【问题讨论】:
-
要从字典中获取值,只需使用 StatusMapping[key]
-
@jdweng:这不是 OP 想要做的。