<?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>Fremus.co.za &#187; IEnumerable</title>
	<atom:link href="http://www.fremus.co.za/blog/tag/ienumerable/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fremus.co.za/blog</link>
	<description>Demistifying Life and Web Development</description>
	<lastBuildDate>Sun, 27 Nov 2011 15:56:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using yield to stream database results</title>
		<link>http://www.fremus.co.za/blog/2009/10/using-yield-to-stream-database-results/</link>
		<comments>http://www.fremus.co.za/blog/2009/10/using-yield-to-stream-database-results/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 15:19:47 +0000</pubDate>
		<dc:creator>fr3dr1k</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[IEnumerable]]></category>
		<category><![CDATA[yield]]></category>

		<guid isPermaLink="false">http://www.fremus.co.za/blog/?p=495</guid>
		<description><![CDATA[I have been looking for a way to stream database results for a while now, and has as yet not come to a fitting solution. That is until today and after I did some reading on the yield keyword. The yield keyword can be used inside a function that implements the IEnumerable interface and what [...]]]></description>
			<content:encoded><![CDATA[<p>I have been looking for a way to stream database results for a while now, and has as yet not come to a fitting solution. That is until today and after I did some reading on the yield keyword. The yield keyword can be used inside a function that implements the IEnumerable interface and what it seems to do is force a loop to return a result even though its not finished. Lets say for instance you have a function like this:</p>
<pre name="code" class="csharp">
    public static IEnumerable Power(int number, int exponent)
    {
        int counter = 0;
        int result = 1;
        while (counter++ < exponent)
        {
            result = result * number;
            yield return result;
        }
    }
</pre>
<p>And you implement it like this:</p>
<pre name="code" class="csharp">

         //Display powers of 2 up to the exponent 8:
        foreach (int i in Power(2, 9))
        {
            Console.Write("{0}", i + "\n");
            Thread.Sleep(2000);
        }
</pre>
<p>You will see that the results in the command line are delayed. If you take that same logic and you translate it to a function that queries a database like this:</p>
<pre name="code" class="csharp">
    public static IEnumerable<string> GetData()
    {
        DataTable table = new DataTable();
        using (SqlConnection sqlConnection = new SqlConnection("connectionstring"))
        {
            string commandText = "select chattext from Chat";
            using (SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection))
            {
                sqlCommand.CommandType = CommandType.Text;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCommand);
                dataAdapter.Fill(table);
            }

        }
        foreach (DataRow row in table.Rows)
        {
            yield return row["chattext"].ToString();
        }
    }
</pre>
<p>Then the implementation is similar to the first example's:</p>
<pre name="code" class="csharp">

        foreach (string s in GetData())
        {
            Console.WriteLine(s + "\n");
            Thread.Sleep(2000);
        }
</pre>
<p>And the result is the same. Read about the <a href="http://msdn.microsoft.com/en-us/library/9k7k7cf0(VS.80).aspx" onclick="urchinTracker('/outgoing/msdn.microsoft.com/en-us/library/9k7k7cf0_VS.80_.aspx?referer=');">yield keyword on MSDN</a> and check out <a href="http://stackoverflow.com/questions/803878/c-ienumerator-yield-structure-potentially-bad" onclick="urchinTracker('/outgoing/stackoverflow.com/questions/803878/c-ienumerator-yield-structure-potentially-bad?referer=');">this StackOverflow article</a>, on which the database example is based.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save" onclick="urchinTracker('/outgoing/www.addtoany.com/share_save?referer=');"><img src="http://www.fremus.co.za/blog/wp-content/plugins/add-to-any/favicon.png" width="16" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.fremus.co.za/blog/2009/10/using-yield-to-stream-database-results/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

