You need to use the lambda operator, which means not using a query expression. In this case, I would not use a query expression, given that you only have a choice ...
List<twoWords> twoWords = stringlist.Select(words => { var ret = new twoWords(); ret.setvalues(words); return ret; }) .ToList();
Or, alternatively, just enter a method that returns the corresponding twoWords :
private static twoWords CreateTwoWords(string words) { var ret = new twoWords(); ret.setvalues(words); return ret; } List<twoWords> twoWords = stringlist.Select(CreateTwoWords) .ToList();
This will also allow you to use a query expression if you really wanted to:
List<twoWords> twoWords = (from words in stringlist select CreateTwoWords(words)).ToList();
Of course, another option would be to give the twoWords constructor a constructor that did the right thing, and at that point you just would not need to call the method ...
source share