Skip to content

Using LINQ ForEach To Populate Generic List in C# AND Enum to List

Updated: at 02:12 PM

A C# trick I first had trouble wrapping my head around is using the LINQ ForEach operator to populate a list.  I was originally inspired by some source I found in the EXT.NET Icon building sample.   In addition to the ForEach lamda trick they included a very nice pattern for converting a c# Enum to a List.  So, let’s get right to the code.

 

enum Days { Sat = 1, Sun, Mon, Tue, Wed, Thu, Fri };
static void Main(string[] args)
{
    List<string> daysOfWeekList = Enum.GetNames(typeof(Days)).ToList();
    daysOfWeekList.ForEach(a => Console.WriteLine(a));
}

 

That’s it!   Just another two patterns of using c# which helps me live cleaner in c#!

HTH’s.

Check out the ORM (Object Relational Mapper) PRISMA. The database access method I use in all my projects