<?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, 17 Aug 2010 20:34:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.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>
<pre lang="PHP">
public List<ItemResults> FindResults()
 {

    var query = from items in db.TimePeriod_Items
    group items by items.TimePeriod into g
    orderby g.Key.finish
    select new ItemResults
    {
        TimePeriod = g.Key,
        TimePeriod_Item = g.Key.TimePeriod_Items.OrderByDescending(item => item.votes).Take(3)
    };

    return query.ToList();

 }
</pre>
<p>ItemResult Class</p>
<pre lang="PHP">
public class ItemResults
{
    public TimePeriod TimePeriod { get; set; }
    public IEnumerable<TimePeriod_Item> TimePeriod_Item { get; set; }
}
</pre>
<p>Use in ASP.NET MVC</p>
<pre lang="PHP">
public ActionResult Results()
{
    var items = appRepository.FindResults();
    return View("Results", items);
}
</pre>
]]></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>
