我有一個可以包含以下值泛型列表:
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