2

Question Updated

I have a generic list which can contains the following values:

Sector 1
Sector 2
Sector 4

or the following value:

All Sectors

I want to format this values in a string like this:

Sector 1 & 2 & 4 - 

or

All Sectors -

Currently, I have the following code to format the same. It works, but is very complicated.

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))) + " - "

In the above code, the variable sector is the generic list. Can someone help me to attain the results in a simplified way.? Or may be modify the above code so that it is more understandable.

Any help appreciated. Thanks

5
  • How many sectors does exist??? Commented Mar 23, 2011 at 19:06
  • Its mentioned in the updated question above. The list may contain "All Sectors" or "Sector 1, Sector 2, Sector 4" or it may be empty. I need a solution for all three scenarios. Please check the updated question Commented Mar 23, 2011 at 19:08
  • Think I've got the definitive answer, take a look below! Commented Mar 23, 2011 at 19:16
  • 1
    Hey Reggie, what it was doing was first checking if the string was empty. If it was then it did nothing. Otherwise it would order the sector list by the last char in the string. Then Group by the sector and selects the first which gets rid of the duplicates. Then it aggregates the string together where it first checks to see if current = retrievesectors which means theres nothing in the string, so that would be the first one. Otherwise it just adds the next strings with a & and the sector number in between. I realize it was complicated but thats the only way I could get it to work. Commented Jul 6, 2011 at 17:03
  • Thanks for the comment Gage. With the help of the answers below, I was successful to shrink in the code. I actually used a combination of the below 2 posts to attain it. :) Commented Jul 6, 2011 at 17:09

3 Answers 3

1

Maybe a little more simple:

    string retrieveSectors =
        string.Format(
        "sectors {0} -",
        sectors.Select(s => s.Replace("sector ", "").Replace("|", ""))
            .OrderBy(s => s)
            .Aggregate((a, b) => string.Format("{0} & {1}", a, b))
        );
Sign up to request clarification or add additional context in comments.

1 Comment

The solution does not work if "All Sectors" come in play. Please check the question, I have updated it again. Sorry about the previous one.
1

Try this out!

List<String> list = new List<String>() { "Sector 1", "Sector 2", "Sector 4" };

(list.Count == 0 ? "Not any sector " :    
((list.Contains("All Sectors") ? "All Sectors " :
    "Sector " + String.Join(" & ", list.OrderBy(c => c).ToArray())
        .Replace("Sector", String.Empty)))) + " - "

Also work for:

List<String> list = new List<String>();
List<String> list = new List<String>() { "All Sectors" };

Comments

0

Assuming source is any kind of IEnumerable<string> where Sectors are stored, probably the more concise syntax is :

String.Join(" & ", source).Replace(" Sector ", " ")

Or this, if source might be unordered and you want ordered sector numbers :

String.Join(" & ", source.OrderBy(s => s)).Replace(" Sector ", " ")

And finally, ultimate refinement to check for "no sector at all" like in Renato's answer :

source.Any() ? String.Join(" & ", source.OrderBy(s => s)).Replace(" Sector ", " ") : "No sectors"

All of these solutions work anyway in the 2 cases you mentioned at first (simply, the 2 latter are enhanced versions to deal with addtional cases which might reasonably occur and interest you).

1 Comment

Doesn't look too bad, I like the use of Aggregate in kevev22 answer though. I wonder which of the solutions is more performant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.