2

I cannot find out if this is possible, and if I want to push lambda too far. I do not like the double regex (the Class.Column is not mine). I have this simple select function :

(ColumNames is a List)

string reg = "(.*):(.*)";
Class.Column[] Columns = (Class.Column[])this.ColumnNames
    .Select(x =>
        new Class.Column() {
            Param1 = Regex.Match(x, reg).Groups[1].ToString(),
            Param2 = Regex.Match(x, reg).Groups[2].ToString()
        }
    );

Is there a way to set the regex output as z, then param1 = z1.Groups[1].ToString()?

1
  • why not just x.Split(':') Commented Feb 3, 2017 at 10:34

1 Answer 1

2

You can linq multiple Select to perform multiple transformations. You also have to replace the cast by a ToArray feature

        Class.Column[] Columns = this.ColumnNames
            .Select(x=> Regex.Match(x, reg))
            .Select(z =>
            new Class.Column()
            {
                Param1 = z.Groups[1].ToString(),
                Param2 = z.Groups[2].ToString()
            }).ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

I think the .ToArray() is necessary. The casting of the result of .Select(...) to an array will fail since it is not an array.
You are right. What I wanted to say is that the correct way of doing this is using the ToArray() feature, not that it would be preferable.

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.