﻿
{"id":2362,"date":"2013-11-23T09:45:50","date_gmt":"2013-11-23T09:45:50","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=2362"},"modified":"2013-11-23T09:45:50","modified_gmt":"2013-11-23T09:45:50","slug":"c-ile-lambda-expression-olan-extension-metod-yazmak","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/c-ile-lambda-expression-olan-extension-metod-yazmak\/","title":{"rendered":"C# \u0130le Lambda Expression Olan Extension Metod Yazmak"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>\u00d6nceki yaz\u0131lar\u0131mdan <a href=\"http:\/\/www.gencayyildiz.com\/blog\/extension-metod-yazilisi-ve-kullanimi\/\" target=\"_blank\">Extension Metod Yaz\u0131l\u0131\u015f\u0131 ve Kullan\u0131m\u0131<\/a> ba\u015fl\u0131kl\u0131 yaz\u0131mda Extension metodun nas\u0131l yaz\u0131ld\u0131\u011f\u0131na ve kullan\u0131ld\u0131\u011f\u0131na de\u011finmi\u015ftik.Ancak bir Extension metoda Lambda Expression ifadelerin nas\u0131l tan\u0131mland\u0131\u011f\u0131ndan bahsetmedik.Haliyle bu yaz\u0131m\u0131zda Lambda Expression dahil olan bir Extension metodu yazmay\u0131 ve kullanmay\u0131 \u00f6rneklendirece\u011fiz.<br \/>\n<!--more--><br \/>\n\u00d6ncelikle bir Extension metod nas\u0131l olu\u015fturuluyordu hat\u0131rlayal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    static class ExtensionOrnek\r\n    {\r\n        public static bool Ornek(this int sayi)\r\n        {\r\n            return true;\r\n        }\r\n    }\r\n<\/pre>\n<p>Yukar\u0131daki \u00f6rnekte g\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi en sade bir bi\u00e7imde Extension metod bulunmaktad\u0131r.Extension metodlar sadece static s\u0131n\u0131flar i\u00e7erisinde tan\u0131mlanabiliyorlard\u0131.Metodun parametresinde bulunan &#8220;this int sayi&#8221; de\u011feri, bu Extension metodun sadece &#8220;int&#8221; tipinden de\u011ferlerde \u00e7al\u0131\u015faca\u011f\u0131n\u0131 s\u00f6ylemektedir.<\/p>\n<p>Peki biz nas\u0131l Lambda Expression dahil olan Extension metodlar yazaca\u011f\u0131z diye sorarsan\u0131z e\u011fer \u00f6ncelikle <a href=\"http:\/\/www.gencayyildiz.com\/blog\/c-funconceden-tanimlanmis-delege-turu\/\" target=\"_blank\">C# \u2013 Func(\u00d6nceden Tan\u0131mlanm\u0131\u015f Delege T\u00fcr\u00fc)<\/a> ba\u015fl\u0131kl\u0131 yaz\u0131m\u0131 okuman\u0131z\u0131 tavsiye ederim.<\/p>\n<p>\u015eimdi bir \u00f6rneklendirme yapal\u0131m.<br \/>\nSadece koleksiyonlar \u00fczerinde \u00e7al\u0131\u015fan iki Extension metod yazal\u0131m ve Lambda Expression ile birinde Where \u015fart\u0131n\u0131, di\u011ferinde Single olarak veri getirecek sorgulama ger\u00e7ekle\u015ftirelim.<\/p>\n<p>Tabi \u00f6ncelikle bir Entity olu\u015ftural\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class Doktor\r\n    {\r\n        public int ID { get; set; }\r\n        public string Adi { get; set; }\r\n        public string SoyAdi { get; set; }\r\n        public bool AmeliyatVarmi { get; set; }\r\n        public int AmeliyatSayisi { get; set; }\r\n    }\r\n<\/pre>\n<p>G\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi bir doktor tan\u0131mlad\u0131m.\u015eimdi Extension metodlar\u0131m\u0131z\u0131 yazal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    static class Extension where T : class\r\n    {\r\n        public static IEnumerable ExtWhere(this IEnumerable List, Func&lt;T, bool&gt; LambdaExpression)\r\n        {\r\n            List Gelenler = new List();\r\n            foreach (T item in List)\r\n            {\r\n                if (LambdaExpression(item))\r\n                {\r\n                    Gelenler.Add(item);\r\n                }\r\n            }\r\n            return Gelenler;\r\n        }\r\n\r\n        public static T ExtFirstOrDefault&lt;T&gt;(this IEnumerable&lt;T&gt; List, Func&lt;T, bool&gt; LambdaExpression) where T : class\r\n        {\r\n            foreach (T item in List)\r\n            {\r\n                if (LambdaExpression(item))\r\n                {\r\n                    return item;\r\n                }\r\n            }\r\n\r\n            return null;\r\n        }\r\n    }\r\n<\/pre>\n<p>Evet, g\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi Extension metodlar\u0131m\u0131z\u0131 yazm\u0131\u015f olduk.&#8221;.NET&#8221; mimarisinde bulunan &#8220;Where&#8221; ve &#8220;FirstOrDefault&#8221; metodlar\u0131yla kar\u0131\u015fmamalar\u0131 i\u00e7in metod isimlerinin ba\u015f\u0131na &#8220;Ext&#8221; getirdim.<\/p>\n<p>\u015eimdi siz Extension metodlar\u0131 incelerken ben burada nas\u0131l bir mant\u0131kta \u00e7al\u0131\u015faca\u011f\u0131m\u0131z\u0131 anlatay\u0131m.Metodlar\u0131m\u0131za bakarsan\u0131z e\u011fer hemen hemen i\u00e7lerinde ayn\u0131 yap\u0131lar\u0131 bar\u0131nd\u0131rmaktad\u0131rlar.Bu Extensionlar kullan\u0131ld\u0131klar\u0131 koleksiyonu this parametresi sayesinde getirmektedirler.Gelen bu koleksiyonun \u00fczerinde metod i\u00e7inde i\u015flemler ger\u00e7ekle\u015ftirilmektedir.\u0130\u015fte bu metodlar\u0131n i\u015flemlerini tam olarak ger\u00e7ekle\u015ftirebilmesi i\u00e7in &#8220;LambdaExpression&#8221; isimli parametremize bir \u015fartlar\u0131 kontrol edecek olan metod ba\u011flamam\u0131z gerekmektedir.Bu ba\u011flayaca\u011f\u0131m\u0131z metodu da Extension metodu kulland\u0131\u011f\u0131m\u0131z yerde, Lambda Expression ifadeleriyle sa\u011flayaca\u011f\u0131z.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            List&lt;Doktor&gt; Doktorlar = new List&lt;Doktor&gt; { \r\n                new Doktor{ ID=1, Adi = &quot;Ali&quot;, SoyAdi=&quot;Y\u0131ld\u0131z&quot;, AmeliyatSayisi = 10, AmeliyatVarmi = false },\r\n                new Doktor{ ID=2, Adi = &quot;Gen\u00e7ay&quot;, SoyAdi=&quot;Y\u0131ld\u0131z&quot;, AmeliyatSayisi = 2, AmeliyatVarmi = false },\r\n                new Doktor{ ID=3, Adi = &quot;Emine&quot;, SoyAdi=&quot;Y\u0131ld\u0131z&quot;, AmeliyatSayisi = 20, AmeliyatVarmi = true },\r\n                new Doktor{ ID=4, Adi = &quot;G\u00fcl\u015fah&quot;, SoyAdi=&quot;Y\u0131ld\u0131z&quot;, AmeliyatSayisi = 17, AmeliyatVarmi = false },\r\n                new Doktor{ ID=5, Adi = &quot;Sebahattin&quot;, SoyAdi=&quot;Y\u0131ld\u0131z&quot;, AmeliyatSayisi = 40, AmeliyatVarmi = true },\r\n                new Doktor{ ID=6, Adi = &quot;Nurettin&quot;, SoyAdi=&quot;Y\u0131ld\u0131z&quot;, AmeliyatSayisi = 40, AmeliyatVarmi = true },\r\n            };\r\n\r\n            var Gelenler = Doktorlar.ExtWhere(d =&gt; d.AmeliyatSayisi &gt;17);\r\n\r\n            foreach (var item in Gelenler)\r\n            {\r\n                Console.WriteLine(item.Adi + &quot; &quot; + item.SoyAdi);\r\n            }\r\n\r\n            Console.Read();\r\n        }\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelerseniz e\u011fer &#8220;ExtWhere&#8221; metodunu denemi\u015f olduk.Lambda \u0130fadeleriyle \u015fart verece\u011fimiz metodu da bu \u015fekilde tan\u0131mlad\u0131ktan sonra projeyi derleyip \u00e7al\u0131\u015ft\u0131rd\u0131\u011f\u0131m\u0131z zaman ald\u0131\u011f\u0131m\u0131z sonu\u00e7 a\u015fa\u011f\u0131daki gibi olacakt\u0131r.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2013\/11\/ExtensionLambdaExpression.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2013\/11\/ExtensionLambdaExpression.png\" alt=\"ExtensionLambdaExpression\" width=\"140\" height=\"43\" class=\"aligncenter size-full wp-image-2371\" \/><\/a><br \/>\nSon olarak &#8220;ExtFirstOrDefault&#8221; metodumuzu deneyelim.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            List&lt;Doktor&gt; Doktorlar = new List&lt;Doktor&gt; { \r\n                new Doktor{ ID=1, Adi = &quot;Ali&quot;, SoyAdi=&quot;Y\u0131ld\u0131z&quot;, AmeliyatSayisi = 10, AmeliyatVarmi = false },\r\n                new Doktor{ ID=2, Adi = &quot;Gen\u00e7ay&quot;, SoyAdi=&quot;Y\u0131ld\u0131z&quot;, AmeliyatSayisi = 2, AmeliyatVarmi = false },\r\n                new Doktor{ ID=3, Adi = &quot;Emine&quot;, SoyAdi=&quot;Y\u0131ld\u0131z&quot;, AmeliyatSayisi = 20, AmeliyatVarmi = true },\r\n                new Doktor{ ID=4, Adi = &quot;G\u00fcl\u015fah&quot;, SoyAdi=&quot;Y\u0131ld\u0131z&quot;, AmeliyatSayisi = 17, AmeliyatVarmi = false },\r\n                new Doktor{ ID=5, Adi = &quot;Sebahattin&quot;, SoyAdi=&quot;Y\u0131ld\u0131z&quot;, AmeliyatSayisi = 40, AmeliyatVarmi = true },\r\n                new Doktor{ ID=6, Adi = &quot;Nurettin&quot;, SoyAdi=&quot;Y\u0131ld\u0131z&quot;, AmeliyatSayisi = 40, AmeliyatVarmi = true },\r\n            };\r\n\r\n            var GelenDoktor = Doktorlar.ExtFirstOrDefault(d =&gt; d.ID == 2);\r\n            Console.WriteLine(GelenDoktor.Adi + &quot; &quot; + GelenDoktor.SoyAdi);\r\n            Console.Read();\r\n        }\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funun i\u015flem sonucu a\u015fa\u011f\u0131dad\u0131r.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2013\/11\/ExtensionLambdaExpression2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2013\/11\/ExtensionLambdaExpression2.png\" alt=\"ExtensionLambdaExpression2\" width=\"106\" height=\"17\" class=\"aligncenter size-full wp-image-2373\" \/><\/a><\/p>\n<p>Extension metodlar yaz\u0131l\u0131msal olarak esneklik pay\u0131 sa\u011flad\u0131\u011f\u0131ndan dolay\u0131 en \u00e7ok tercih edilen tekniklerden biri oldu\u011funu bilmenizi isterim&#8230;Extension metodlar b\u00fcy\u00fck i\u015flerin alt\u0131ndan kalkabilece\u011finden dolay\u0131 kaliteli bir Extension sizin i\u00e7in iyi bir CV bile olabilir \ud83d\ude42<\/p>\n<p>\u0130yi \u00e7al\u0131\u015fmalar diliyorum..<\/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>\u00d6nceki yaz\u0131lar\u0131mdan Extension Metod Yaz\u0131l\u0131\u015f\u0131 ve Kullan\u0131m\u0131 ba\u015fl\u0131kl\u0131 yaz\u0131mda Extension metodun nas\u0131l yaz\u0131ld\u0131\u011f\u0131na ve kullan\u0131ld\u0131\u011f\u0131na de\u011finmi\u015ftik.Ancak bir Extension metoda Lambda Expression ifadelerin nas\u0131l tan\u0131mland\u0131\u011f\u0131ndan bahsetmedik.Haliyle bu yaz\u0131m\u0131zda Lambda Expression dahil olan bir Extension metodu&#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":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,338],"tags":[27,232,532],"class_list":["post-2362","post","type-post","status-publish","format-standard","hentry","category-c-sharp-c","category-c-5-0","tag-c","tag-extension","tag-lambda-expression"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/2362","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=2362"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/2362\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=2362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=2362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=2362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}