<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Using LINQ to foreach over an enum in C#</title>
	<atom:link href="http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c/feed" rel="self" type="application/rss+xml" />
	<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-linq-to-foreach-over-an-enum-in-c</link>
	<description>A .NET developer in silicon valley</description>
	<lastBuildDate>Fri, 18 May 2012 15:31:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Jesse Pakin</title>
		<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c#comment-42631</link>
		<dc:creator>Jesse Pakin</dc:creator>
		<pubDate>Tue, 09 Nov 2010 04:43:42 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=901#comment-42631</guid>
		<description>Thanks for the post. I was able to use it along with &lt;a href=&quot;http://weblogs.asp.net/stefansedich/archive/2008/03/12/enum-with-string-values-in-c.aspx&quot; rel=&quot;nofollow&quot;&gt;another posting regarding custom attributes
&lt;/a&gt; to list out friendly names for enums in a dropdown, in a view in an MVC project (with a nice bit of LINQ).

&lt;pre&gt;&lt;code&gt;&lt;%: Html.DropDownList(&quot;CardRecord.Type&quot;,((IList&lt;CreditCardType&gt;)Enum.GetValues(typeof(CreditCardType))).Select(s =&gt; new SelectListItem{Text = s.GetStringValue(), Value = s.ToString()}))%&gt;&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Thanks for the post. I was able to use it along with <a href="http://weblogs.asp.net/stefansedich/archive/2008/03/12/enum-with-string-values-in-c.aspx" rel="nofollow">another posting regarding custom attributes<br />
</a> to list out friendly names for enums in a dropdown, in a view in an MVC project (with a nice bit of LINQ).</p>
<pre><code>&lt;%: Html.DropDownList("CardRecord.Type",((IList&lt;CreditCardType&gt;)Enum.GetValues(typeof(CreditCardType))).Select(s =&gt; new SelectListItem{Text = s.GetStringValue(), Value = s.ToString()}))%&gt;</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Florent</title>
		<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c#comment-8263</link>
		<dc:creator>Florent</dc:creator>
		<pubDate>Tue, 15 Apr 2008 14:56:12 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=901#comment-8263</guid>
		<description>Hi guys :)
I think that &quot;Cast&quot; method is not mandatory... In fact, you can directly cast that Enum.GetValues return into IEnumerable. Because it&#039;s a simple array :)
So you can do like this:
&lt;pre&gt;&lt;code&gt;IEnumerable&lt;T&gt; myEnumerableOfEnum = (IEnumerable&lt;T&gt;)Enum.GetValues(typeof(T));&lt;/code&gt;&lt;/pre&gt;
But you can do this too:
&lt;pre&gt;&lt;code&gt;IList&lt;T&gt; myListOfEnum = (IList&lt;T&gt;)Enum.GetValues(typeof(T));
T[] myArrayOfEnum = (T[])Enum.GetValues(typeof(T));
&lt;/code&gt;&lt;/pre&gt;

I hope this will useful :)</description>
		<content:encoded><![CDATA[<p>Hi guys :)<br />
I think that &#8220;Cast&#8221; method is not mandatory&#8230; In fact, you can directly cast that Enum.GetValues return into IEnumerable. Because it&#8217;s a simple array :)<br />
So you can do like this:</p>
<pre><code>IEnumerable&lt;T&gt; myEnumerableOfEnum = (IEnumerable&lt;T&gt;)Enum.GetValues(typeof(T));</code></pre>
<p>But you can do this too:</p>
<pre><code>IList&lt;T&gt; myListOfEnum = (IList&lt;T&gt;)Enum.GetValues(typeof(T));
T[] myArrayOfEnum = (T[])Enum.GetValues(typeof(T));
</code></pre>
<p>I hope this will useful :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard</title>
		<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c#comment-8242</link>
		<dc:creator>Richard</dc:creator>
		<pubDate>Sun, 13 Apr 2008 07:37:21 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=901#comment-8242</guid>
		<description>&gt; hope you don&#039;t mind me reformatting it

No problem.

This is the second time I would have liked to write an extension method that extends the target&#039;s static methods (rather than instance).</description>
		<content:encoded><![CDATA[<p>&gt; hope you don&#8217;t mind me reformatting it</p>
<p>No problem.</p>
<p>This is the second time I would have liked to write an extension method that extends the target&#8217;s static methods (rather than instance).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard</title>
		<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c#comment-8238</link>
		<dc:creator>Richard</dc:creator>
		<pubDate>Sat, 12 Apr 2008 15:56:01 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=901#comment-8238</guid>
		<description>You can make use of an extension method to make this slightly nicer... but you call the extension against an instance of the enum (naming for the extension needs to be better):
&lt;pre&gt;&lt;code&gt;
&lt;strong&gt;using&lt;/strong&gt; System;
&lt;strong&gt;using&lt;/strong&gt; System.Collections.Generic;
&lt;strong&gt;using&lt;/strong&gt; System.Linq;

&lt;strong&gt;static class&lt;/strong&gt; Enums {
	&lt;strong&gt;public static &lt;/strong&gt;IEnumerable&lt;T&gt; Values&lt;T&gt;(&lt;strong&gt;this&lt;/strong&gt; T en) &lt;strong&gt;where&lt;/strong&gt; T : &lt;strong&gt;struct&lt;/strong&gt; {
		&lt;strong&gt;if&lt;/strong&gt; (!&lt;strong&gt;typeof&lt;/strong&gt;(T).IsEnum)
			&lt;strong&gt;throw new&lt;/strong&gt; InvalidOperationException();
		&lt;strong&gt;foreach&lt;/strong&gt; (&lt;strong&gt;var&lt;/strong&gt; x &lt;strong&gt;in&lt;/strong&gt;Enum.GetValues(typeof(T)).Cast&lt;T&gt;())
			&lt;strong&gt;yield return&lt;/strong&gt; x;
	}
}

&lt;strong&gt;enum&lt;/strong&gt; Test { One, Two, Three }

&lt;strong&gt;class&lt;/strong&gt; Program {
	&lt;strong&gt;static void&lt;/strong&gt; Main(&lt;strong&gt;string&lt;/strong&gt;[] args) {
		IEnumerable&lt;Test&gt; t = Test.One.Values();
		&lt;strong&gt;foreach&lt;/strong&gt; (&lt;strong&gt;var&lt;/strong&gt; x &lt;strong&gt;in&lt;/strong&gt; t)
			Console.WriteLine(x);
	}
}
&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>You can make use of an extension method to make this slightly nicer&#8230; but you call the extension against an instance of the enum (naming for the extension needs to be better):</p>
<pre><code>
<strong>using</strong> System;
<strong>using</strong> System.Collections.Generic;
<strong>using</strong> System.Linq;

<strong>static class</strong> Enums {
	<strong>public static </strong>IEnumerable&lt;T&gt; Values&lt;T&gt;(<strong>this</strong> T en) <strong>where</strong> T : <strong>struct</strong> {
		<strong>if</strong> (!<strong>typeof</strong>(T).IsEnum)
			<strong>throw new</strong> InvalidOperationException();
		<strong>foreach</strong> (<strong>var</strong> x <strong>in</strong>Enum.GetValues(typeof(T)).Cast&lt;T&gt;())
			<strong>yield return</strong> x;
	}
}

<strong>enum</strong> Test { One, Two, Three }

<strong>class</strong> Program {
	<strong>static void</strong> Main(<strong>string</strong>[] args) {
		IEnumerable&lt;Test&gt; t = Test.One.Values();
		<strong>foreach</strong> (<strong>var</strong> x <strong>in</strong> t)
			Console.WriteLine(x);
	}
}
</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Damien Guard</title>
		<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c#comment-8239</link>
		<dc:creator>Damien Guard</dc:creator>
		<pubDate>Sat, 12 Apr 2008 09:30:01 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=901#comment-8239</guid>
		<description>That&#039;s nice! (hope you don&#039;t mind me reformatting it).  I did consider going with an extension method but my worry was having .Values appear for all classes.

[)amien</description>
		<content:encoded><![CDATA[<p>That&#8217;s nice! (hope you don&#8217;t mind me reformatting it).  I did consider going with an extension method but my worry was having .Values appear for all classes.</p>
<p>[)amien</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c#comment-8203</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Thu, 10 Apr 2008 21:25:13 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=901#comment-8203</guid>
		<description>Yuck. Equivalent Java code:

for (CustomerTypes customerType : CustomerTypes.values())

One of the rare occasions Java is more concise than C#, obviously ;)</description>
		<content:encoded><![CDATA[<p>Yuck. Equivalent Java code:</p>
<p>for (CustomerTypes customerType : CustomerTypes.values())</p>
<p>One of the rare occasions Java is more concise than C#, obviously ;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Damien Guard</title>
		<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c#comment-8199</link>
		<dc:creator>Damien Guard</dc:creator>
		<pubDate>Thu, 10 Apr 2008 19:56:37 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=901#comment-8199</guid>
		<description>Good question!

LINQ uses some specific compiler magic for the select/in/where/order keywords and also has a bunch of classes in the System.Linq namespace.  

To achieve the functionality requires these classes use Extension methods quite extensively.  It also takes advantage of Lambda expressions, anonymous types and is demonstrated often using type inference, all C# 3.0 features!

[)amien</description>
		<content:encoded><![CDATA[<p>Good question!</p>
<p>LINQ uses some specific compiler magic for the select/in/where/order keywords and also has a bunch of classes in the System.Linq namespace.  </p>
<p>To achieve the functionality requires these classes use Extension methods quite extensively.  It also takes advantage of Lambda expressions, anonymous types and is demonstrated often using type inference, all C# 3.0 features!</p>
<p>[)amien</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Guy</title>
		<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c#comment-8198</link>
		<dc:creator>Guy</dc:creator>
		<pubDate>Thu, 10 Apr 2008 19:26:18 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=901#comment-8198</guid>
		<description>Great post and valuable information - bookmarked and will be used in the future. :)

Question: Is it actually the power of LINQ that you&#039;re using? i.e. Are extension methods part of LINQ or are they part of C# 3.0?</description>
		<content:encoded><![CDATA[<p>Great post and valuable information &#8211; bookmarked and will be used in the future. :)</p>
<p>Question: Is it actually the power of LINQ that you&#8217;re using? i.e. Are extension methods part of LINQ or are they part of C# 3.0?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

