Yoyo
Yoyo

Reputation: 101

linq lambda multiple steps array

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()?

Upvotes: 2

Views: 277

Answers (1)

Bruno Belmondo
Bruno Belmondo

Reputation: 2367

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();

Upvotes: 2

Related Questions