{"id":2241,"date":"2014-07-29T19:03:20","date_gmt":"2014-07-29T19:03:20","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2241"},"modified":"2014-07-30T09:54:49","modified_gmt":"2014-07-30T09:54:49","slug":"query-expressions-in-f","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/query-expressions-in-f\/","title":{"rendered":"Query expressions in F#"},"content":{"rendered":"<p>Not quite the same as LINQ in syntax terms, F# includes query expressions which are in essence it&#8217;s own style of LINQ.<\/p>\n<p>If we assume we have a simple example of some code in C# which uses LINQ to get strings from an array where their length is 3. In C# this might look like the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstring&#x5B;] sample = {&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;, &quot;Four&quot;, &quot;Five&quot;};\r\n\r\nIEnumerable&lt;string&gt; result = \r\n   from s in sample \r\n   where s.Length == 3 \r\n   select s;\r\n<\/pre>\n<p><em>Note: I&#8217;ve purposefully put each part of the LINQ query on a new line for easier comparison with the equivalent F# query expression.<\/em><\/p>\n<p>So here&#8217;s the same in F#<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet sample = &#x5B;| &quot;One&quot; ; &quot;Two&quot; ; &quot;Three&quot; ; &quot;Four&quot; ; &quot;Five&quot; |]\r\n\r\nlet result = query {\r\n   for s in sample do\r\n   where (s.Length = 3)\r\n   select s\r\n}\r\n<\/pre>\n<p>The main differences are the use of the query expression syntax <em>query { &#8230; }<\/em> and the use of the <em> for s in sample do<\/em> instead of <em>from s in sample<\/em><\/p>\n<p><strong>References<\/strong><\/p>\n<p>See <a href=\"http:\/\/msdn.microsoft.com\/en-gb\/library\/hh225374.aspx\" title=\"Query Expressions (F#)\" target=\"_blank\">Query Expressions (F#)<\/a> for a few examples of different query expressions and how they relate to SQL queries (and more).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Not quite the same as LINQ in syntax terms, F# includes query expressions which are in essence it&#8217;s own style of LINQ. If we assume we have a simple example of some code in C# which uses LINQ to get strings from an array where their length is 3. In C# this might look like [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-2241","post","type-post","status-publish","format-standard","hentry","category-f"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=2241"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2241\/revisions"}],"predecessor-version":[{"id":2248,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2241\/revisions\/2248"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}