{"id":222,"date":"2010-02-08T15:02:58","date_gmt":"2010-02-08T20:02:58","guid":{"rendered":"http:\/\/hoolihan.net\/blog-tim\/?p=222"},"modified":"2010-02-08T20:57:28","modified_gmt":"2010-02-09T01:57:28","slug":"functions-instead-of-predicates-in-linq-functions","status":"publish","type":"post","link":"http:\/\/hoolihan.net\/blog-tim\/2010\/02\/08\/functions-instead-of-predicates-in-linq-functions\/","title":{"rendered":"Function Name Instead of Lambda in Linq Functions"},"content":{"rendered":"<p>I did not realize that functions can fill in for predicates directly without lambda notation.  To illustrate, consider the following:<\/p>\n<pre>\r\n<code>void Main()\r\n{\r\n\tvar words = new List&lt;string&gt;()\r\n\t{\r\n\t\t\"therapists\",\r\n\t\t\"s words\",\r\n\t\t\"slang\",\r\n\t\t\"mustache\",\r\n\t\t\"sean connery\"\r\n\t};\r\n\t\r\n\tvar s_words = words.Where(w =&gt; w.StartsWith(\"s\" ,StringComparison.CurrentCultureIgnoreCase));\r\n\t\r\n\tforeach(var word in s_words)\r\n\t{\r\n\t\tConsole.WriteLine(word);\r\n\t}\r\n}<\/code>\r\n<\/pre>\n<p>It works, but it&#8217;s a rather long lambda, which is why I broke it out of the for loop.  Let&#8217;s put that logic into a function, getting this:<\/p>\n<pre>\r\n<code>void Main()\r\n{\r\n\tvar words = new List&lt;string&gt;()\r\n\t{\r\n\t\t\"therapists\",\r\n\t\t\"s words\",\r\n\t\t\"slang\",\r\n\t\t\"mustache\",\r\n\t\t\"sean connery\"\r\n\t};\r\n\t\r\n\tforeach(var word in words.Where(w =&gt; SWords(w)))\r\n\t{\r\n\t\tConsole.WriteLine(word);\r\n\t}\r\n}\r\n\r\npublic bool SWords(string word)\r\n{\r\n\treturn word.StartsWith(\"s\" ,StringComparison.CurrentCultureIgnoreCase);\r\n}<\/code>\r\n<\/pre>\n<p>That&#8217;s better.  And you might find yourself using your test in other places in the code, so it&#8217;s useful to have the function.  What I found out recently, is that you can go one step further:<\/p>\n<pre>\r\n<code>void Main()\r\n{\r\n\tvar words = new List&lt;string&gt;()\r\n\t{\r\n\t\t\"therapists\",\r\n\t\t\"s words\",\r\n\t\t\"slang\",\r\n\t\t\"mustache\",\r\n\t\t\"sean connery\"\r\n\t};\r\n\t\r\n\tforeach(var word in words.Where(SWords))\r\n\t{\r\n\t\tConsole.WriteLine(word);\r\n\t}\r\n}\r\n\r\npublic bool SWords(string word)\r\n{\r\n\treturn word.StartsWith(\"s\" ,StringComparison.CurrentCultureIgnoreCase);\r\n}<\/code>\r\n<\/pre>\n<p>In this example, the savings may not look drastic.  But for several chained methods you can gain a lot of brevity and clarity.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I did not realize that functions can fill in for predicates directly without lambda notation. To illustrate, consider the following: void Main() { var words = new List&lt;string&gt;() { &#8220;therapists&#8221;, &#8220;s words&#8221;, &#8220;slang&#8221;, &#8220;mustache&#8221;, &#8220;sean connery&#8221; }; var s_words = words.Where(w =&gt; w.StartsWith(&#8220;s&#8221; ,StringComparison.CurrentCultureIgnoreCase)); foreach(var word in s_words) { Console.WriteLine(word); } } It works, but [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[41,4,69,83,272],"class_list":["post-222","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-net","tag-computers","tag-linkedin","tag-linq","tag-programming"],"_links":{"self":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/comments?post=222"}],"version-history":[{"count":0,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/222\/revisions"}],"wp:attachment":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/media?parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/categories?post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/tags?post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}