Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Linq extension on Array class

I'm trying to build a SelectList from an Enum.

Why are the Linq extension methods not available on Array?

var values = Enum.GetValues(typeof(MyEnum));
var test = values.Select(x => x); // compile error

But I can write it this way and it compiles...

var test = from Enum e in values select new { e };

I don't normally use this style of syntax so I'm not really familiar with it, but isn't the above essentially the same as the lambda query which doesn't compile?

like image 443
fearofawhackplanet Avatar asked Apr 20 '11 11:04

fearofawhackplanet


1 Answers

Use OfType method to get an IEnumerable<T> that can be queried using LINQ:

var values = Enum.GetValues(typeof(MyEnum));
var test = values.OfType<int>().Select(x => x);
like image 128
volpav Avatar answered Oct 17 '22 04:10

volpav