0

I would like to run GetResults() on all possible combinations of groups, languages and TestEnum. How can I re-write this code using lambda expressions to avoid using the foreach loops?

    public enum TestEnum
    {
        ONE,
        TWO,
        THREE
    }

    public static void Test1()
    {
        List<string> groups = new List<string>() { "group1", "group2", "group3", "group4" };
        List<string> languages = new List<string>() { "EN-CA", "FR-CA" };
        List<string> results = new List<string>();

        foreach (string group in groups)
        {
            foreach (string language in languages)
            {
                foreach (TestEnum enumval in Enum.GetValues(typeof(TestEnum)))
                {
                    results.Add(GetResult(group, language, enumval));
                }
            }
        }

    }
    public static string GetResult(string group, string language, TestEnum enumval)
    {
        return group + language + enumval.ToString();
    }
2
  • Rather than calling GetValues and then getting the string value of that, you can just use GetNames (that is, unless your real code uses the actual values, not just the string representations). Commented Oct 31, 2013 at 20:25
  • I'd recommend the trial version of Resharper, which will convert them for you, which helped me learn very quickly Commented Oct 31, 2013 at 20:26

3 Answers 3

6

I think query syntax here is better than lambdas:

results = (from g in groups
           from l in languages
           from e in Enum.GetValues(typeof(TestEnum)).Cast<TestEnum>()
           select g + l + e).ToList();

Produces

group1EN-CAONE
group1EN-CATWO
group1EN-CATHREE
group1FR-CAONE
group1FR-CATWO
group1FR-CATHREE
group2EN-CAONE
group2EN-CATWO
group2EN-CATHREE
etc
Sign up to request clarification or add additional context in comments.

3 Comments

I would say clearer, not better, given that they compile into the same thing. But yes, this would look a lot uglier in method syntax.
@Servy thnaks, yes you are right, cleaner is appropriate word)
I agree as well that query syntax is much clearer, my intent in the question was to use method syntax so Ill go with that answer.
2

Hope this solve your problem:

var results = (
    from @group in groups 
    from language in languages 
    from TestEnum enumval in Enum.GetValues(typeof (TestEnum)) 
         select GetResult(@group, language, enumval)).ToList();

Let me know if your need some explanation.

Comments

1

I tend to agree with lazyberezovsky, query syntax is easier to read in this case, but here goes:

results = groups
  .SelectMany(group => languages
     .SelectMany(language => Enum.GetValues(typeof(TestEnum))
        .Cast<TestEnum>()
           .Select(enumValue => GetResult(group, language, enumValue))))
  .ToList();

Maybe with better indentation or line breaks it would look nicer...

1 Comment

query syntax is much clearer, my intent in the question was to use method syntax though, thanks

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.