<?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>Social Vitamin &#187; take</title>
	<atom:link href="http://www.socialvitamin.com/tag/take/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.socialvitamin.com</link>
	<description>Give your social space a vitamin boost.</description>
	<lastBuildDate>Tue, 20 Sep 2011 17:02:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Group By, Group, Take and Order By with LinQ in C#</title>
		<link>http://www.socialvitamin.com/2009/05/13/group-by-group-take-and-order-by-with-linq-in-c/</link>
		<comments>http://www.socialvitamin.com/2009/05/13/group-by-group-take-and-order-by-with-linq-in-c/#comments</comments>
		<pubDate>Wed, 13 May 2009 21:04:20 +0000</pubDate>
		<dc:creator>Michael D. Irizarry</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[group]]></category>
		<category><![CDATA[limit]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[order by]]></category>
		<category><![CDATA[take]]></category>

		<guid isPermaLink="false">http://www.socialvitamin.com/?p=128</guid>
		<description><![CDATA[At some point you&#8217;ll need to group and order your results using LINQ. It&#8217;s an easy task but a little bit hard to master since you have a few options. [...]]]></description>
			<content:encoded><![CDATA[<p>At some point you&#8217;ll need to group and order your results using LINQ. It&#8217;s an easy task but a little bit hard to master since you have a few options. Lets imagine we have Voting Poll that people will vote on periods. The voting Poll has 3 tables TimePeriod, Items and the Cross Reference table TimePeriod_Items and we want to build a results grid using them. The best option for me is the following. Notice I&#8217;m also limiting the results using the Take method this way I only have 3 items per Group. Also notice that I&#8217;m not returning an Anonymous Type but rather an ItemResults Type.</p>
<p>Code</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>ItemResults<span style="color: #339933;">&gt;</span> FindResults<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">var</span> query <span style="color: #339933;">=</span> from items in db<span style="color: #339933;">.</span>TimePeriod_Items
    group items by items<span style="color: #339933;">.</span>TimePeriod into g
    orderby g<span style="color: #339933;">.</span><span style="color: #990000;">Key</span><span style="color: #339933;">.</span>finish
    select <span style="color: #000000; font-weight: bold;">new</span> ItemResults
    <span style="color: #009900;">&#123;</span>
        TimePeriod <span style="color: #339933;">=</span> g<span style="color: #339933;">.</span><span style="color: #990000;">Key</span><span style="color: #339933;">,</span>
        TimePeriod_Item <span style="color: #339933;">=</span> g<span style="color: #339933;">.</span><span style="color: #990000;">Key</span><span style="color: #339933;">.</span>TimePeriod_Items<span style="color: #339933;">.</span>OrderByDescending<span style="color: #009900;">&#40;</span>item <span style="color: #339933;">=&gt;</span> item<span style="color: #339933;">.</span>votes<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>Take<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> query<span style="color: #339933;">.</span>ToList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #009900;">&#125;</span></pre></div></div>

<p>ItemResult Class</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ItemResults
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> TimePeriod TimePeriod <span style="color: #009900;">&#123;</span> get<span style="color: #339933;">;</span> set<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> IEnumerable<span style="color: #339933;">&lt;</span>TimePeriod_Item<span style="color: #339933;">&gt;</span> TimePeriod_Item <span style="color: #009900;">&#123;</span> get<span style="color: #339933;">;</span> set<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Use in ASP.NET MVC</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> ActionResult Results<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> items <span style="color: #339933;">=</span> appRepository<span style="color: #339933;">.</span>FindResults<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> View<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Results&quot;</span><span style="color: #339933;">,</span> items<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.socialvitamin.com/2009/05/13/group-by-group-take-and-order-by-with-linq-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

