I am new to lambda expressions; I am learning by implementing them. I have a question on how to convert a for-loop into a lambda expression.
EnumHelper.GetEnumFromString is a helper method that takes the string description and sends back the enum.
[Flags]
public enum Colors
{
[DescriptionAttribute("YL")]
Yellow = 1,
[DescriptionAttribute("RD")]
Red = 2,
[DescriptionAttribute("GR")]
Green = 4
}
string colorStr = "GR,RD";
List<Colors> clrs = colorStr.Split(new char[] { ',' }).Select(p => EnumHelper.GetEnumFromString<Colors>(p)).ToList();
Colors currentValidColors = Colors.Green;
for (int i = 0; i < clrs .Count; i++)
{
if (i == 0)
currentValidColors = clrs [i];
else
currentValidColors = currentValidColors | clrs [i];
}
Is this what you are thinking of? ForEach extension method is only available for generic lists.