{"id":4099,"date":"2014-03-30T07:42:34","date_gmt":"2014-03-30T07:42:34","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/linq-select-expression-ienumerable-collection-of-common-programming-errors\/"},"modified":"2014-03-30T07:42:34","modified_gmt":"2014-03-30T07:42:34","slug":"linq-select-expression-ienumerable-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/linq-select-expression-ienumerable-collection-of-common-programming-errors\/","title":{"rendered":"LINQ Select expression IEnumerable-Collection of common programming errors"},"content":{"rendered":"<p>Please have a look at Why would you use Expression rather than Func?.<\/p>\n<p>The accepted answer describes that <code>Func<\/code> is a delegate to a method that returns a <code>T<\/code>. <code>Expression<\/code> however is actually a <strong>description<\/strong> of how that delegate can be evaluated. You can for example compile an expression into an actual delegate:<\/p>\n<pre><code>Expression expr = i =&gt; i &lt; 5;\nFunc deleg = expr.Compile();\nConsole.WriteLine(\"deleg(4) = {0}\", deleg(4));\n<\/code><\/pre>\n<p>You can even write:<\/p>\n<pre><code>Console.WriteLine(\"deleg(4) = {0}\", expr.Compile()(4));\n<\/code><\/pre>\n<p>So if you really need an expression you need to compile it into the actual delegate otherwise use the delegate in the first place.<\/p>\n<p>(BTW: Your code example won&#8217;t compile since there is no <code>Select<\/code> method for a <code>List<\/code> that takes an expression and also <code>d =&gt; d.user.username<\/code> is probably wrong. Since <code>d<\/code> is a <code>user<\/code> it should be <code>d =&gt; d.username<\/code>).<\/p>\n<p>However linq to entities does not support delegate calls. Therefore you have to switch to <strong>linq to objects<\/strong> by adding <code>AsEnumerable()<\/code>:<\/p>\n<pre><code>Expression expr = d =&gt; d.username;\nFunc func = expr.Compile();\n\nvar result = context.Message.AsEnumerable()\n                            .Select(b =&gt; new { name = b.user.Select(func) });\n<\/code><\/pre>\n<p>Use linq to entities for accessing the data, simple ordering, filtering etc. This way it can optimize the queries for the database but then switch to linq to objects if you need more.<\/p>\n<p>PS: The lambda <code>d =&gt; d.username<\/code> is compiled into a delegate. If you explicitly put it into the expression <code>b.user.Select(u =&gt; u.username)<\/code> it will work fine because now this also compiles into an expression that linq to entities can handle without calling a delegate.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Please have a look at Why would you use Expression rather than Func?. The accepted answer describes that Func is a delegate to a method that returns a T. Expression however is actually a description of how that delegate can be evaluated. You can for example compile an expression into an actual delegate: Expression expr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4099","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4099"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4099\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}