<?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>Codepolice&#187; linq</title>
	<atom:link href="http://codepolice.net/category/linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://codepolice.net</link>
	<description>Random posts from Ola in English. Mainly about programming and the web.</description>
	<lastBuildDate>Tue, 24 Jan 2012 07:19:43 +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>IN queries with LINQ to SQL</title>
		<link>http://codepolice.net/in-queries-with-linq-to-sql/</link>
		<comments>http://codepolice.net/in-queries-with-linq-to-sql/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 13:06:12 +0000</pubDate>
		<dc:creator>Ola</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://in-queries-with-linq-to-sql</guid>
		<description><![CDATA[This is another awesome feature in LINQ to SQL that i always forget about. Sometimes you have an array of strings or any other type and want to query the database for all values that have one of the values in the array. string platform = &#34;windows&#124;linux&#34;; string&#91;&#93; platformList = platform.Split&#40;'&#124;'&#41;; itemQuery = from m [...]]]></description>
			<content:encoded><![CDATA[<p>This is another awesome feature in LINQ to SQL that i always forget about. Sometimes you have an array of strings or any other type and want to query the database for all values that have one of the values in the array.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">string</span> platform <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;windows|linux&quot;</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> platformList <span style="color: #008000;">=</span> platform<span style="color: #008000;">.</span><span style="color: #0000FF;">Split</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">'|'</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
itemQuery <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">from</span> m <span style="color: #0600FF; font-weight: bold;">in</span> itemQuery
<span style="color: #0600FF; font-weight: bold;">from</span> p <span style="color: #0600FF; font-weight: bold;">in</span> m<span style="color: #008000;">.</span><span style="color: #0000FF;">PlattformItemRelations</span>
<span style="color: #0600FF; font-weight: bold;">where</span> platformList<span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span>p<span style="color: #008000;">.</span><span style="color: #0000FF;">Plattform</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToLower</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #0600FF; font-weight: bold;">select</span> m<span style="color: #008000;">;</span></pre></div></div>

<p>Ain&#8217;t that a beauty? I got this tip from <a href="http://blog.wekeroad.com/2008/02/27/creating-in-queries-with-linq-to-sql/">Ron Connery</a> so all the kudos to him.</p>
]]></content:encoded>
			<wfw:commentRss>http://codepolice.net/in-queries-with-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Join and Group By with LINQ to Entities</title>
		<link>http://codepolice.net/join-and-group-by-linq-to-entities/</link>
		<comments>http://codepolice.net/join-and-group-by-linq-to-entities/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 16:11:55 +0000</pubDate>
		<dc:creator>Ola</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://join-and-group-by-linq-to-entities</guid>
		<description><![CDATA[I have banged my head against some LINQ to Entities stuff today. It started out when i was doing a query to get some stats out of my forum. I should point out that i&#8217;m a newbe with LINQ to Entities and the Entity Framework so if i have done anything stupid here you are [...]]]></description>
			<content:encoded><![CDATA[<p>I have banged my head against some LINQ to Entities stuff today. It started out when i was doing a query to get some stats out of my forum. I should point out that i&#8217;m a newbe with LINQ to Entities and the Entity Framework so if i have done anything stupid here you are free to mock me in the comments.</p>
<pre>var query = (from s in db.ForumStatsSet
where s.LogDate &gt;= date1 &amp;&amp; s.LogDate &lt;= date2
group s by new {
  s.Topic.topicID, s.Topic.subject,
  s.Topic.datum, s.Topic.Forum.forumID,
  s.Topic.Forum.forumName,
  s.Topic.Forum.ForumGroup.name } into g
orderby g.Count() descending
select new TopicStatsData
  {
  TopicId = g.Key.topicID,
  Count = g.Count(),
  Subject = g.Key.subject,
  ForumId = g.Key.forumID,
  ForumName = g.Key.forumName,
  ForumGroupName = g.Key.name
});</pre>
<p>This code looked fine to me. It&#8217;s kind of massive and a lot of joins but it should be used in a admin interface so i did not care that much. But it turned out that it timeout every time in ran it. I had a look at the SQL that was generated and that was not a pretty sight. A massive amount of sub queries and left outer joins.</p>
<p>I <a href="http://stackoverflow.com/questions/317281/optimze-group-by-in-linq-to-entities">posted a question</a> on the new and really really good code community <a href="http://stackoverflow.com/">Stackoverflow</a> and got the tip that i should join the tables before group by clue. And that is what i tried to do.</p>
<p>The problem was that i got this error all the time.</p>
<p><em>from s in db.ForumStatsSet<br />
from t in s.Topic</em></p>
<p><em>Error:<br />
An expression of type &#8216;WhoaAdmin.Models.Topic&#8217; is not allowed in a subsequent from clause in a query expression with source type &#8216;System.Data.Objects.ObjectQuery&lt;WhoaAdmin.Models.ForumStats&gt;&#8217;. Type inference failed in the call to &#8216;SelectMany&#8217;.</em></p>
<p>I tried to google that error and i got nothing. I realised that maybe you have to do the joins in the correct order and that was it and i ended up with this query.</p>
<pre>var query = (from fg in db.ForumGroupSet
from f in fg.Forums
from t in f.Topics
from s in t.ForumStats
where s.LogDate &gt;= date1 &amp;&amp; s.LogDate &lt;= date2
group s by new { t.topicID,
  t.subject,
  t.datum,
  f.forumID,
  f.forumName,
  fg.name } into g
orderby g.Count() descending
select new TopicStatsData
{
 TopicId = g.Key.topicID,
 Count = g.Count(),
 Subject = g.Key.subject,
 ForumId = g.Key.forumID,
 ForumName = g.Key.forumName,
 ForumGroupName = g.Key.name
});</pre>
<p>The SQL i ended up with looks kind of awful but probably the SQL engine do something like this &#8220;under the hood&#8221; on regular SQL group by to. At least it is kind of fast. The big diffrence in the generated SQL is that it has replaced all &#8220;LEFT OUTER JOINS&#8221; with &#8220;INNER JOINS&#8221; and they are of course faster.</p>
<p>Please leave comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://codepolice.net/join-and-group-by-linq-to-entities/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: www.codepolice.net @ 2012-02-07 07:49:12 -->
