<?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>San Jose Web Design &#124; San Francisco Website Design &#124; Development Company &#187; bcherry</title>
	<atom:link href="http://www.xillent.com/author/bcherry/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xillent.com</link>
	<description>Xillent Studios</description>
	<lastBuildDate>Mon, 16 Feb 2026 06:54:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Custom .NET Collection Filters with C# Extension Methods</title>
		<link>http://www.xillent.com/blog/codesharing/c/custom-net-collection-filters-with-c-sharp-extension-methods/</link>
		<comments>http://www.xillent.com/blog/codesharing/c/custom-net-collection-filters-with-c-sharp-extension-methods/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 20:11:44 +0000</pubDate>
		<dc:creator>bcherry</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[examples]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://blog.xillent.com/?p=141</guid>
		<description><![CDATA[<p>Have you ever had a collection (it doesn&#8217;t matter what kind of collection), and done some filtering on it using LINQ methods? For example, the following: This is a pretty common usage of the LINQ extension method Where(). The above &#8230; <a href="http://www.xillent.com/blog/codesharing/c/custom-net-collection-filters-with-c-sharp-extension-methods/" class="readMore">Read More</a></p><p>The post <a href="http://www.xillent.com/blog/codesharing/c/custom-net-collection-filters-with-c-sharp-extension-methods/">Custom .NET Collection Filters with C# Extension Methods</a> appeared first on <a href="http://www.xillent.com">San Jose Web Design | San Francisco Website Design | Development Company</a>.</p>]]></description>
			<content:encoded><![CDATA[<p>Have you ever had a collection (it doesn&#8217;t matter what kind of collection), and done some filtering on it using LINQ methods?  For example, the following:</p>
<p>List&lt;product&gt; products = GetAllProducts();<br />
var inStock = products.Where(p =&gt; p.Qty &gt; 0);<br />
foreach(Product p in inStock)<br />
{<br />
//dosomething with the products<br />
}</p>
<p>This is a pretty common usage of the LINQ extension method Where().  The above would work for any collection type, including List&lt;&gt;, IList&lt;&gt;, IEnumerable&lt;&gt;, IQueryable&lt;&gt;, or any collection with those handy LINQ extensions.</p>
<p>Anyways, it turns out you can actually write your own extension methods!  Check this out:</p>
<p>public static IEnumerable&lt;product&gt; WhereInStock(this IEnumerable&lt;product&gt; p)<br />
{<br />
return p.Where(x =&gt; x.Qty &gt; 0);<br />
}</p>
<p>//&#8230; skip ahead to some code where we use the above</p>
<p>var products = GetAllProducts();<br />
var inStock = products.WhereInStock();</p>
<p>Ok, so that&#8217;s pretty cool.  But the really neat thing this lets you do is chaining:</p>
<p>foreach(Product p in GetProducts().WhereInStock().WhereOnSale())<br />
{<br />
//do something<br />
}</p>
<p>Of course, this method will let me grab all of the products, but filter them down based on their qty and their sale status.  The real benefit here is separation of concerns.  This code may be used in the UI layer somewhere.  The UI coder doesn&#8217;t need to know how to determine if a product is on sale (it could be a complicated calculation), and he <strong>shouldn&#8217;t need to</strong>.  By building a common set of filter methods for your domain objects, you&#8217;ll make life a lot easier for other team members.</p>
<p>Note: I haven&#8217;t actually compiled this code above, let me know if I made a typo!</p>
<p><a href="http://www.developer.com/net/csharp/article.php/3592216">Here&#8217;s more on implementing extension methods</a></p>
<p>[this post cross-posted with permission from <a href="http://bcherry.net">bcherry.net</a>] </p>
<p>The post <a href="http://www.xillent.com/blog/codesharing/c/custom-net-collection-filters-with-c-sharp-extension-methods/">Custom .NET Collection Filters with C# Extension Methods</a> appeared first on <a href="http://www.xillent.com">San Jose Web Design | San Francisco Website Design | Development Company</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.xillent.com/blog/codesharing/c/custom-net-collection-filters-with-c-sharp-extension-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
