Resolved Enums in Linq

Ant6729

Well-known member
Joined
Jan 22, 2019
Messages
56
Programming Experience
Beginner
Hello, I try to refactor this
C#:
var collection = new List<ShowOfCurrentEnum>();

            foreach (var dealStatus in (DealStatuses[])Enum.GetValues(typeof(DealStatuses)))
            {
                var showCurrentEnum = new ShowOfCurrentEnum
                {
                    Description = dealStatus.GetEnumDescription(),

                    IndexValue = (int) dealStatus,

                    NameOfValue = dealStatus.ToString()
                };

                collection.Add(showCurrentEnum);
            }

I try to iterate like this,

C#:
            var myNewCollection = (DealStatuses[]) Enum.GetValues(typeof(DealStatuses)).Select(fg=>
                new ShowOfCurrentEnum()
                {
                    Description = fg.GetEnumDescription(),

                    IndexValue = (int)fg,

                    NameOfValue = fg.ToString()
                }).ToList();


but have no result.
 
Solution
Right now you're trying to Select directly on Enum.GetValues which return System.Array.
'Array' does not contain a definition for 'Select' and no accessible extension method 'Select' accepting a first argument of type 'Array' could be found

Put another paranthesis around the enum array cast: (...).Select
Right now you're trying to Select directly on Enum.GetValues which return System.Array.
'Array' does not contain a definition for 'Select' and no accessible extension method 'Select' accepting a first argument of type 'Array' could be found

Put another paranthesis around the enum array cast: (...).Select
 
Solution
Beat me to it.
 
When things go wrong in complicated expressions it can be a good idea to break in down into multiple statements:
C#:
var values = (DealStatuses[]) Enum.GetValues(typeof(DealStatuses));
var myNewCollection = values.Select(fg=>
 
Back
Top Bottom