There is a list
var list = new List<string>() {"12", "23", "34"}; var val1 = ""; var val2 = ""; var val3 = ""; How using lambda or linq to put variables from the list in val1, val2, val3. I'll do it in a loop, but I want a more elegant solution.
There is a list
var list = new List<string>() {"12", "23", "34"}; var val1 = ""; var val2 = ""; var val3 = ""; How using lambda or linq to put variables from the list in val1, val2, val3. I'll do it in a loop, but I want a more elegant solution.
I can offer a solution based on DLR (.NET 4.0+):
public static class Exts { public static dynamic ExtractIndices<T> (this IEnumerable<T> @this, string propertyPrefix) { CultureInfo culture = CultureInfo.InvariantCulture; var variables = new ExpandoObject(); var dictionary = (IDictionary<string, object>)variables; int index = 1; using (IEnumerator<T> enumerator = @this.GetEnumerator()) { while (enumerator.MoveNext()) { string propertyName = string.Format(culture, "{0}{1}", propertyPrefix, index++); dictionary[propertyName] = enumerator.Current; } } return variables; } } Using:
var list = new List<string> { "12", "23", "34" }; var v = list.ExtractIndices("al"); Console.WriteLine("{0} {1} {2}", v.al1, v.al2, v.al3); The disadvantages include an extra point in the name of "variables", but is this a hindrance?
PS What is the question, such an answer.
static dynamic . - VladDnew unsafe async ... - AthariSource: https://ru.stackoverflow.com/questions/425931/
All Articles