2 回答
TA贡献1772条经验 获得超5个赞
我会将其拆分以使其更具可读性:
public static class HeaderOperators
{
public static IDictionary<string, string> GetValues(IReadOnlyList<string> keys, IHeaderDictionary headers, string defaultValue)
{
var firstCollection = keys
.Select(x => x.ToLower()).Intersect(headers.Keys.Select(x => x.ToLower()))
.Select(k => new KeyValuePair<string, string>(k.ToLower(), headers[k.ToLower()]));
var secondCollection = keys
.Select(x => x.ToLower())
.Where(k => !headers.Keys.Select(x => x.ToLower()).Contains(k.ToLower()))
.Select(k => new KeyValuePair<string, string>(k.ToLower(), defaultValue));
return
firstCollection.Union(secondCollection).ToDictionary(p => p.Key.ToLower(), p => p.Value);
}
}
TA贡献1817条经验 获得超14个赞
由于它比嵌套更连锁,我只需将其格式化为如下所示:
return keys.Select( x => x.ToLower() ) .Intersect( headers.Keys.Select(x => x.ToLower()) ) .Select( k => new KeyValuePair<string, string>(k.ToLower(), headers[k.ToLower()]) ) .Union( keys.Select(x => x.ToLower() ) .Where( k => !headers.Keys.Select(x => x.ToLower()).Contains(k.ToLower()) ) .Select( k => new KeyValuePair<string, string>(k.ToLower(), defaultValue)) ) .ToDictionary( p => p.Key.ToLower(), p => p.Value );
并且...在每行末尾添加注释。
- 2 回答
- 0 关注
- 114 浏览
添加回答
举报