﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programming By A Tool &#187; ForEach</title>
	<atom:link href="http://byatool.com/tag/foreach/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>This is my recorded stumbling through ASP.Net, Framework 3.5, C# 3.0, Ajax, Javascript, and Love.  Stay, learn, destroy.  It's your life.</description>
	<lastBuildDate>Thu, 03 May 2012 17:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Using the ForEach method on List Collections</title>
		<link>http://byatool.com/general-coding/using-the-foreach-method-on-list-collections/</link>
		<comments>http://byatool.com/general-coding/using-the-foreach-method-on-list-collections/#comments</comments>
		<pubDate>Thu, 26 Jun 2008 13:18:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[ForEach]]></category>
		<category><![CDATA[Lambda]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/06/26/using-the-foreach-method-on-list-collections/</guid>
		<description><![CDATA[public static StateCollection GetByName(String partialName) { StateCollection returnValue; returnValue = new StateCollection(); //baseList is some list I wasn't nice enough to show where it came from. //It's just a list of states. Get over it. var stateList = from state in baseList where state.Name.StartsWith(partialName) select new State(state.Name, state.Code); stateList.ToList().ForEach(currentState =&#62; returnValue.Add(currentState)); return returnValue; } So [...]]]></description>
			<content:encoded><![CDATA[<pre><strong>
public static StateCollection GetByName(String partialName)
{
 StateCollection returnValue;

 returnValue = new StateCollection();

<span style="color: #009900;">  //baseList is some list I wasn't nice enough to show where it came from.</span>
<span style="color: #009900;">  //It's just a list of states.  Get over it.</span>
 var stateList = from state in baseList
                 where state.Name.StartsWith(partialName)
                 select new State(state.Name, state.Code);

 stateList.ToList().ForEach(currentState =&gt; returnValue.Add(currentState));

 return returnValue;
}
</strong></pre>
<p>So what is done here:</p>
<p>Basically I am using a linq expression to get states from a list of states (Like Michigan, Illinois, or Canada) based on name. No big deal. Then I take the query and produce a List from it. AMAZIN!</p>
<pre><strong>
stateList.ToList().<span style="color: #339999;">ForEach(curretState =&gt; returnValue.Add(curretState));</span>
</strong></pre>
<p>ForEach is an extension method for List&lt;&gt;. Now if you remember from previous posts, that means it resides in a static class somewhere that &#8220;tacks&#8221; it on to the List&lt;&gt; class. Basically this method acts like a typical foreach loop. (You know, go through the list and do something. Or nothing. I suppose you could just loop through if you wanted to. I won&#8217;t tell you how to live your life.) Simple but in my opinion, much cleaner looking.</p>
<p>I mean I could do this:</p>
<pre><strong>
public static StateCollection GetByName(String partialName)
{
 StateCollection returnValue;

 returnValue = new StateCollection();

 foreach(State currentState in baseList)
 {
   if(currentState.Name.StartsWith(partialName)
   {
     returnValue.Add(currentState);
   }
 }

 return returnValue;
}
</strong></pre>
<p>Really it&#8217;s just up to what you prefer. (And I&#8217;m sure you could drive a car with no power brakes. You&#8217;ll still get there.) Also, I really didn&#8217;t need the linq expression since I could have done this all with ForEach (Provided baseList is IEnumerable).</p>
<p>One last note, all IEnumerable collections have the ToList() method (And a bunch of other ones for that matter.)</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/using-the-foreach-method-on-list-collections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

