我有一个可以包含以下值泛型列表:
Sector 1
Sector 2
Sector 4
或以下值:
All Sectors
我想这样的字符串格式如下:
Sector 1 & 2 & 4 -
或
All Sectors -
目前,我有以下的代码格式化一样。它有效,但非常复杂。
string retrieveSectors += sectors.Count == 0
? string.Empty
: sectors.OrderBy(
y =>
y.Sector.Substring(
y.Sector.Length - 1, 1)).
GroupBy(g => g.Sector).Select(
g => g.First()).ToList().Aggregate(
retrieveSectors,
(current, y) =>
(current == retrieveSectors
? current +
y.Sector
: current + " & " +
y.Sector.
Substring(
y.Sector.
Length - 1, 1))) + " - "
在上面的代码中,变量扇区是通用列表。有人能帮助我以简单的方式获得结果吗?或者可以修改上面的代码,使其更易于理解。
任何帮助表示赞赏。由于
有多少个扇区存在? – renatoargh 2011-03-23 19:06:36
它在上面更新的问题中提到。该列表可能包含“所有部门”或“部门1,部门2,部门4”,或者可能为空。我需要针对所有三种场景的解决方案。请检查更新的问题 – reggie 2011-03-23 19:08:55
认为我已经得到了明确的答案,请看下面! – renatoargh 2011-03-23 19:16:37