{"id":17,"date":"2019-11-20T20:35:30","date_gmt":"2019-11-20T20:35:30","guid":{"rendered":"http:\/\/144.76.171.171\/blog\/?p=17"},"modified":"2019-11-21T12:02:46","modified_gmt":"2019-11-21T12:02:46","slug":"lambda-expression-nedir-nasil-kullanilir","status":"publish","type":"post","link":"https:\/\/berenkudaygorun.com\/blog\/blog\/2019\/11\/20\/lambda-expression-nedir-nasil-kullanilir\/","title":{"rendered":"Lambda Expression Nedir, Nas\u0131l Kullan\u0131l\u0131r?"},"content":{"rendered":"<p>E\u011fer benim gibi \u00f6nceki projelerinizde nodejs ile \u00e7al\u0131\u015ft\u0131ysan\u0131z asl\u0131nda bu ifadenin Arrow Function\u2019lara benzedi\u011fini d\u00fc\u015f\u00fcnebilirsiniz ve evet syntax olarak \u00e7ok benzemektedir. Bundan dolay\u0131 bu konuyu \u00f6\u011frenirken motivasyonlu bir \u015fekilde ba\u015flam\u0131\u015ft\u0131m. Bu yaz\u0131mda Lambda ifadelerini elimden geldi\u011fince anlatmay\u0131 deneyece\u011fim. \u0130yi okumalar\u2026<\/p>\n<p>\u00d6ncelikle Lambda ifadelerinin anonim birer fonksiyonlar (metot de\u011fil!!!) oldu\u011funu bilmemiz gerekiyor. Peki anonim fonksiyon da ne demek? Bu yap\u0131lar\u0131n bir ismi yoktur diyerek k\u0131sa bir cevap verece\u011fim \u015fimdilik. Bu sayede kod yazarken bize kolayl\u0131k sa\u011flamaktad\u0131rlar. \u015eimdi bunu bir \u00f6rnek \u00fczerinde g\u00f6relim.<\/p>\n<p>Senaryomuzda bir veritaban\u0131na ba\u011flan\u0131p Mr Robot dizisinin 5. Sezon b\u00f6l\u00fcmlerinin puanlar\u0131n\u0131 bir ko\u015fuldan ge\u00e7irerek i\u015flem yapt\u0131\u011f\u0131m\u0131z\u0131 d\u00fc\u015f\u00fcnelim. Puan\u0131 9\u2019dan b\u00fcy\u00fck olan b\u00f6l\u00fcmler ekrana bast\u0131ral\u0131m. E\u011fer Lambda Expression kullanmasayd\u0131k bu i\u015flemi nas\u0131l yapard\u0131k?<\/p>\n<p>Burada bir veritaban\u0131 olu\u015fturmaktan ziyade model \u00fczerine Repository\u2019de verileri ekleyerek \u00e7ekme i\u015flemini tercih ettim.<\/p>\n<pre><code class=\"language-csharp\">namespace LamdaFonksiyonu.Models\n{\n \u00a0\u00a0 public class MrRobot\n \u00a0\u00a0 {\n \u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0public int ID { get; set; }\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 public string Title { get; set; }\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 public float Score { get; set; }\n \u00a0\u00a0 }\n}<\/code><\/pre>\n<p>Model yap\u0131s\u0131 yukar\u0131daki gibidir. Repository k\u0131sm\u0131 a\u015fa\u011f\u0131da verilmi\u015ftir.<\/p>\n<pre><code class=\"language-csharp\">using LamdaFonksiyonu.Models;\nusing System.Collections.Generic;\n\nnamespace LamdaFonksiyonu\n{\n \u00a0\u00a0 public class MrRobotRepository\n \u00a0\u00a0 {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 public List&lt;MrRobot&gt; GetEpisodes()\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return new List&lt;MrRobot&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new MrRobot(){ID=1, Title=&quot;401 Unauthorized&quot;, Score=9.5f},\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new MrRobot(){ID=2, Title=&quot;402 Payment Required&quot;, Score=8.9f},\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new MrRobot(){ID=3, Title=&quot;403 Forbidden&quot;, Score=8.8f},\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new MrRobot(){ID=4, Title=&quot;404 Not Found&quot;, Score=8.8f},\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new MrRobot(){ID=5, Title=&quot;405 Method Not Allowed&quot;, Score=9.8f},\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new MrRobot(){ID=6, Title=&quot;406 Not Acceptable&quot;, Score=9.2f},\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new MrRobot(){ID=7, Title=&quot;407 Proxy Authentication Required&quot;, Score=10f},\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 };\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\n \u00a0\u00a0 }\n}<\/code><\/pre>\n<p>Ve Program.cs ise a\u015fa\u011f\u0131daki gibidir.<\/p>\n<pre><code class=\"language-csharp\">using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\nusing LamdaFonksiyonu.Models;\n\nnamespace LamdaFonksiyonu\n{\n \u00a0\u00a0 class Program\n \u00a0\u00a0 {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 static void Main(string[] args)\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var MrRobotEpisodes = new MrRobotRepository().GetEpisodes();\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var episodes = MrRobotEpisodes.FindAll(GreaterThan9);\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 foreach (var episode in episodes)\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.WriteLine(&quot;B\u00f6l\u00fcm Ad\u0131: {0}, Puan\u0131: {1}&quot;,episode.Title, episode.Score);\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private static bool GreaterThan9(MrRobot obj)\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return obj.Score &gt; 9f;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\n \u00a0\u00a0 }\n}<\/code><\/pre>\n<p>Program\u0131n \u00e7\u0131kt\u0131s\u0131:<\/p>\n<pre>B\u00f6l\u00fcm Ad\u0131: 401 Unauthorized, Puan\u0131: 9,5\nB\u00f6l\u00fcm Ad\u0131: 405 Method Not Allowed, Puan\u0131: 9,8\nB\u00f6l\u00fcm Ad\u0131: 406 Not Acceptable, Puan\u0131: 9,2\nB\u00f6l\u00fcm Ad\u0131: 407 Proxy Authentication Required, Puan\u0131: 10\n\nPress any key to continue . . .<\/pre>\n<p>\u015eimdi Lambda ifadelerini kullanarak bu i\u015flemi yapmay\u0131 deneyelim.<\/p>\n<pre><code class=\"language-csharp\">using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\nusing LamdaFonksiyonu.Models;\n\nnamespace LamdaFonksiyonu\n{\n \u00a0\u00a0 class Program\n \u00a0\u00a0 {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 static void Main(string[] args)\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var MrRobotEpisodes = new MrRobotRepository().GetEpisodes();\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var episodes = MrRobotEpisodes.FindAll(x =&gt; x.Score &gt; 9f);\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 foreach (var episode in episodes)\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.WriteLine(&quot;B\u00f6l\u00fcm Ad\u0131: {0}, Puan\u0131: {1}&quot;,episode.Title, episode.Score);\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\n \u00a0\u00a0 }\n}<\/code><\/pre>\n<p>G\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere sadece <code>var episodes = MrRobotEpisodes.FindAll(x =&gt; x.Score &gt; 9f);<\/code> kodunu yazd\u0131k. Lambda fonksiyonlar\u0131 yazarken kullanm\u0131\u015f oldu\u011fumuz yap\u0131 parametre ve bool d\u00f6necek ifadeden olu\u015fmaktad\u0131r. Sol taraftaki x burada parametremiz ve =&gt; ifadesinin sa\u011f\u0131ndaki ise gerekli \u015fartlar sa\u011fland\u0131\u011f\u0131nda true d\u00f6necek yap\u0131m\u0131z. G\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere bir fonksiyon ismimiz yok ve tek sat\u0131rda bu i\u015flemimizi tamamlad\u0131k. Projelerimizde kolayl\u0131k sa\u011flad\u0131\u011f\u0131ndan dolay\u0131 servislerimizde \u00e7ok\u00e7a kullanabilece\u011fimiz bir yap\u0131. Umar\u0131m anla\u015f\u0131lm\u0131\u015ft\u0131r. Di\u011fer yaz\u0131larda g\u00f6r\u00fc\u015fmek \u00fczere\u2026<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>E\u011fer benim gibi \u00f6nceki projelerinizde nodejs ile \u00e7al\u0131\u015ft\u0131ysan\u0131z asl\u0131nda bu ifadenin Arrow Function\u2019lara benzedi\u011fini d\u00fc\u015f\u00fcnebilirsiniz ve evet syntax olarak \u00e7ok benzemektedir. Bundan dolay\u0131 bu konuyu&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/berenkudaygorun.com\/blog\/blog\/2019\/11\/20\/lambda-expression-nedir-nasil-kullanilir\/\">Devam\u0131n\u0131 oku<span class=\"screen-reader-text\">Lambda Expression Nedir, Nas\u0131l Kullan\u0131l\u0131r?<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[6,8,9],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-c-sharp","tag-lambda","tag-lambda-expressions","tag-lambda-fonksiyonlari","entry"],"_links":{"self":[{"href":"https:\/\/berenkudaygorun.com\/blog\/wp-json\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/berenkudaygorun.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/berenkudaygorun.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/berenkudaygorun.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/berenkudaygorun.com\/blog\/wp-json\/wp\/v2\/comments?post=17"}],"version-history":[{"count":4,"href":"https:\/\/berenkudaygorun.com\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":29,"href":"https:\/\/berenkudaygorun.com\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions\/29"}],"wp:attachment":[{"href":"https:\/\/berenkudaygorun.com\/blog\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/berenkudaygorun.com\/blog\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/berenkudaygorun.com\/blog\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}