<?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>Sage Developers&#039; Blog &#187; linq selectmant cross join</title>
	<atom:link href="http://sage.co.uk/devblog/index.php/tag/linq-selectmant-cross-join/feed/" rel="self" type="application/rss+xml" />
	<link>http://sage.co.uk/devblog</link>
	<description>An area for Sage to engage in discussion with its developer community</description>
	<lastBuildDate>Mon, 06 Feb 2012 15:40:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Quick Tip: LINQ Cross Joins Using Select Many</title>
		<link>http://sage.co.uk/devblog/index.php/2010/03/quick-tip-linq-cross-joins-using-select-many/</link>
		<comments>http://sage.co.uk/devblog/index.php/2010/03/quick-tip-linq-cross-joins-using-select-many/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 14:24:21 +0000</pubDate>
		<dc:creator>John Hulme</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[linq cross join selectmany]]></category>
		<category><![CDATA[linq selectmant cross join]]></category>

		<guid isPermaLink="false">http://sage.co.uk/devblog/?p=295</guid>
		<description><![CDATA[Sometimes we need to work with data that should be grouped by a particular field or set of fields, but where we can&#8217;t be sure that there will always be data in each group.
For example, we might have a list of transactions, each one of which has been posted into a particular period. If I [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes we need to work with data that should be grouped by a particular field or set of fields, but where we can&#8217;t be sure that there will always be data in each group.</p>
<p>For example, we might have a list of transactions, each one of which has been posted into a particular period. If I want to plot these on a chart, or pivot the data by period then I need to ensure that all of the period values are represented in the dataset, even though there may be some periods in which no transactions were posted. If no transactions were posted in period 2, then I want to see a gap in my chart, or a zero value under the &#8220;Period 2&#8243; heading in my pivotted column &#8211; I don&#8217;t just want to ignore it.</p>
<p>One way of achieving this is by cross-joining the relevant set of category headings. Joining is one of the areas where the syntax and behaviour of LINQ is often a little different to SQL, but the behaviours provided are tremendously powerful, and can often simplify queries that would be difficult in SQL.</p>
<p>Cross joins in LINQ can be acheived using the SelectMany syntax. The query fragment below is an example of solving the above issue. Here we cross join nominal accounts to the current year&#8217;s accounting periods, ensuring that however we further group or filter the data, there will always be a value for any given period &#8211; perfect for populating a chart or pivotting.</p>
<p><code>return cxt.NLNominalAccounts<br />
   .SelectMany<br />
   (<br />
      n=&gt;cxt.SYSAccountingPeriods.Where(w=&gt;w.SYSFinancialYear.YearRelativeToCurrentYear==0),<br />
      (n,s)=&gt;<br />
      new<br />
      {<br />
            NLNominalAccountID = n.NLNominalAccountID,<br />
            PeriodYear = ((("FY" + s.SYSFinancialYear.FinancialYearStartDate.Year.ToString().Substring (2, 2)) + "P") + s.PeriodNumber.ToString().PadLeft (2, '0')),<br />
            SYSAccountingPeriodID = s.SYSAccountingPeriodID,<br />
      }<br />
   )<br />
   .GroupJoin<br />
   (<br />
        cxt.NLPostedNominalTrans.DefaultIfEmpty(),<br />
        a=&gt;new{a.NLNominalAccountID,a.SYSAccountingPeriodID},<br />
        n=&gt;new{n.NLNominalAccountID,n.SYSAccountingPeriodID},<br />
        (a,n)=&gt;new<br />
        {<br />
            a.NLNominalAccountID,<br />
            a.PeriodYear,<br />
            a.SYSAccountingPeriodID,<br />
            CreditValue = (n.Count()==0 ? 0 : n.Sum(t=&gt;t.GoodsValueInBaseCurrency &lt; 0 ? -t.GoodsValueInBaseCurrency : 0) ),<br />
            DebitValue = (n.Count()==0 ? 0 : n.Sum(t=&gt;t.GoodsValueInBaseCurrency &gt; 0 ? t.GoodsValueInBaseCurrency : 0) )<br />
        }<br />
    );</code></p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fsage.co.uk%2Fdevblog%2Findex.php%2F2010%2F03%2Fquick-tip-linq-cross-joins-using-select-many%2F&amp;linkname=Quick%20Tip%3A%20LINQ%20Cross%20Joins%20Using%20Select%20Many"><img src="http://sage.co.uk/devblog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://sage.co.uk/devblog/index.php/2010/03/quick-tip-linq-cross-joins-using-select-many/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

