﻿
{"id":5671,"date":"2017-02-19T17:59:09","date_gmt":"2017-02-19T17:59:09","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=5671"},"modified":"2017-02-19T17:59:09","modified_gmt":"2017-02-19T17:59:09","slug":"cta-a-lambda-expression-with-a-statement-body-cannot-be-converted-to-an-expression-tree-hatasi-ve-cozumu","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/cta-a-lambda-expression-with-a-statement-body-cannot-be-converted-to-an-expression-tree-hatasi-ve-cozumu\/","title":{"rendered":"C#&#8217;ta &#8220;A Lambda Expression With A Statement Body Cannot Be Converted To An Expression Tree&#8221; Hatas\u0131 ve \u00c7\u00f6z\u00fcm\u00fc"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Bu i\u00e7eri\u011fimizde C#&#8217;ta Lambda ifadeleri ile \u00e7al\u0131\u015fmam\u0131z\u0131 icra ederken kar\u015f\u0131la\u015fma ihtimalimiz olan &#8220;<em>A Lambda Expression With A Statement Body Cannot Be Converted To An Expression Tree<\/em>&#8221; hatas\u0131 \u00fczerine konu\u015fuyor olaca\u011f\u0131z. Hi\u00e7 vakit kaybetmeden bu hata neden al\u0131nmakta? al\u0131nd\u0131ysa nas\u0131l \u00e7aresine bak\u0131lmakta? sorular\u0131n\u0131 cevaplamaya ba\u015flayal\u0131m.<\/p>\n<p>\u00d6ncelike ortaya bu hatay\u0131 verecek \u00f6rnek bir olay koyal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nclass MyClass\r\n{\r\n    Lisanca.Models.LisancaEntities VeriTabani = new Lisanca.Models.LisancaEntities();\r\n    public void EnDogruLink&lt;T&gt;(string Link) where T : class, new()\r\n    {\r\n        T instance = VeriTabani.Set&lt;T&gt;().FirstOrDefault(k =&gt;\r\n        {\r\n\r\n            return true;\r\n        });\r\n    }\r\n}\r\n<\/pre>\n<p>(<em>\u0130lgili hatay\u0131 yukar\u0131daki \u00f6rnek olay d\u0131\u015f\u0131ndaki herhangi bir senaryo(lar)dan da alabilirsiniz.<\/em>) Dikkat ederseniz yukar\u0131da Lambda ifadeleriyle i\u015flem ger\u00e7ekle\u015ftiren bir kod blo\u011fumuz var. \u0130\u015fte bu tarz bir \u00e7al\u0131\u015fmada konumuz olan hatayla kar\u015f\u0131la\u015fmaktay\u0131z.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/02\/Cta-A-Lambda-Expression-With-A-Statement-Body-Cannot-Be-Converted-To-An-Expression-Tree-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/02\/Cta-A-Lambda-Expression-With-A-Statement-Body-Cannot-Be-Converted-To-An-Expression-Tree-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc.png\" alt=\"C#&#039;ta &quot;A Lambda Expression With A Statement Body Cannot Be Converted To An Expression Tree&quot; Hatas\u0131 ve \u00c7\u00f6z\u00fcm\u00fc\" width=\"607\" height=\"233\" class=\"aligncenter size-full wp-image-5676\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/02\/Cta-A-Lambda-Expression-With-A-Statement-Body-Cannot-Be-Converted-To-An-Expression-Tree-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc.png 607w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/02\/Cta-A-Lambda-Expression-With-A-Statement-Body-Cannot-Be-Converted-To-An-Expression-Tree-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-300x115.png 300w\" sizes=\"auto, (max-width: 607px) 100vw, 607px\" \/><\/a><\/p>\n<p>\u00d6ncelikle bu hatay\u0131 neden ald\u0131\u011f\u0131m\u0131zdan bahsedelim.<br \/>\nLambda ifadeleri IEnumerable tipinden koleksiyon yap\u0131lar\u0131nda kullan\u0131labilmek i\u00e7in olu\u015fturulmu\u015f bir expressiond\u0131r. O y\u00fczden kullan\u0131lmaya \u00e7al\u0131\u015f\u0131lan koleksiyon yap\u0131s\u0131n\u0131n tipi \u00e7ok \u00f6nemlidir. E\u011fer ki bu tip direkt bir IEnumerable yahut 1. dereceden t\u00fcrevi de\u011filse bu hatayla kar\u015f\u0131la\u015f\u0131lmaktad\u0131r.<\/p>\n<p>\u00d6rne\u011fimizde ilgili koleksiyonumuz DbSet tipindedir. Bu tip IEnumerable tipinden t\u00fcremekte ama lokalde yap\u0131sal olarak farkl\u0131l\u0131k g\u00f6stermektedir. Haliyle bu koleksiyonu direkt olarak IEnumerable tipine a\u015fa\u011f\u0131daki gibi \u00e7evirmemiz sorunu ortadan kald\u0131racakt\u0131r. <\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nclass MyClass\r\n{\r\n    Lisanca.Models.LisancaEntities VeriTabani = new Lisanca.Models.LisancaEntities();\r\n    public void EnDogruLink&lt;T&gt;(string Link) where T : class, new()\r\n    {\r\n        T instance = VeriTabani.Set&lt;T&gt;().AsEnumerable().FirstOrDefault(k =&gt;\r\n        {\r\n\r\n            return true;\r\n        });\r\n    }\r\n}\r\n<\/pre>\n<p>Dikkat ederseniz AsEnumerable metodu bu i\u015flem i\u00e7in yeterlidir.<\/p>\n<p>Benzer \u015fekilde ToList metodunuda kullanabilirdik.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nclass MyClass\r\n{\r\n    Lisanca.Models.LisancaEntities VeriTabani = new Lisanca.Models.LisancaEntities();\r\n    public void EnDogruLink&lt;T&gt;(string Link) where T : class, new()\r\n    {\r\n        T instance = VeriTabani.Set&lt;T&gt;().ToList().FirstOrDefault(k =&gt;\r\n        {\r\n\r\n            return true;\r\n        });\r\n    }\r\n}\r\n<\/pre>\n<p>Nihayetinde Lambda ifadesinin kullan\u0131ld\u0131\u011f\u0131 koleksiyon direkt olarak bir IEnumerable tipinde seyretmektedir.<\/p>\n<p>\u0130\u015fte bu sorunun \u00fcstesinden de bu \u015fekilde gelinebilmektedir.<\/p>\n<p>\u015eimdilik esen kal\u0131n&#8230;<\/p>\n<p>Sonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar dilerim&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Bu i\u00e7eri\u011fimizde C#&#8217;ta Lambda ifadeleri ile \u00e7al\u0131\u015fmam\u0131z\u0131 icra ederken kar\u015f\u0131la\u015fma ihtimalimiz olan &#8220;A Lambda Expression With A Statement Body Cannot Be Converted To An Expression Tree&#8221; hatas\u0131 \u00fczerine konu\u015fuyor olaca\u011f\u0131z. Hi\u00e7 vakit kaybetmeden&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":3223,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,258],"tags":[1623,27,1576,1625,260,484,1561,1043,230,1624],"class_list":["post-5671","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-sharp-c","category-hata-cozumleri","tag-asenumerable","tag-c","tag-c-hata","tag-c-lambda","tag-hata","tag-hata-cozumu","tag-hata-ve-cozumleri","tag-ienumerable","tag-lambda","tag-lambda-ifadeleri"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/5671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=5671"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/5671\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/3223"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=5671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=5671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=5671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}