﻿
{"id":4024,"date":"2016-04-14T14:35:05","date_gmt":"2016-04-14T14:35:05","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=4024"},"modified":"2016-04-14T14:35:05","modified_gmt":"2016-04-14T14:35:05","slug":"linq-to-entities-lastordefault-metodunda-notsupportedexception-hatasi","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/linq-to-entities-lastordefault-metodunda-notsupportedexception-hatasi\/","title":{"rendered":"LINQ to Entities &#8211; LastOrDefault Metodunda NotSupportedException Hatas\u0131"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>LINQ to Entities sorgular\u0131ndan LastOrDefault metodu ile elinizdeki veri k\u00fcmesinin\u00a0son eleman\u0131n\u0131 elde etmek istiyor lakin a\u015fa\u011f\u0131daki hatay\u0131 al\u0131yorsan\u0131z \u00e7\u00f6z\u00fcm\u00fc bu i\u00e7eri\u011fimizdedir.<\/p>\n<p style=\"text-align: center;\"><span style=\"color: #ff0000;\"><em><strong>An unhandled exception of type &#8216;System.NotSupportedException&#8217; occurred in EntityFramework.SqlServer.dll<\/strong><\/em><\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #ff0000;\"><em><strong>Additional information: LINQ to Entities does not recognize the method &#8216;xx(System.Linq.IQueryable`1[xx])&#8217; method, and this method cannot be translated into a store expression.<\/strong><\/em><\/span><\/p>\n<p style=\"text-align: left;\">Hatam\u0131z\u0131n g\u00f6rsel hali a\u015fa\u011f\u0131daki gibidir.<\/p>\n<p style=\"text-align: left;\"><a href=\"http:\/\/www.gencayyildiz.com\/blog\/linq-to-entities-lastordefault-metodunda-notsupportedexception-hatasi\/linq-to-entities-lastordefault-metodunda-notsupportedexception-hatasi\/\" rel=\"attachment wp-att-4025\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4025\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/04\/LINQ-to-Entities-LastOrDefault-Metodunda-NotSupportedException-Hatas\u0131.png\" alt=\"LINQ to Entities - LastOrDefault Metodunda NotSupportedException Hatas\u0131\" width=\"449\" height=\"139\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/04\/LINQ-to-Entities-LastOrDefault-Metodunda-NotSupportedException-Hatas\u0131.png 449w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/04\/LINQ-to-Entities-LastOrDefault-Metodunda-NotSupportedException-Hatas\u0131-300x93.png 300w\" sizes=\"auto, (max-width: 449px) 100vw, 449px\" \/><\/a><\/p>\n<p style=\"text-align: left;\">Bu hatay\u0131 almam\u0131z\u0131n sebebi,\u00a0LastOrDefault metoduyla son eleman\u0131n\u0131 elde etmeye \u00e7al\u0131\u015ft\u0131\u011f\u0131m\u0131z elimizdeki veri k\u00fcmesinin bir Generic Koleksiyon yahut Array yap\u0131s\u0131nda veri yap\u0131s\u0131 olmamas\u0131ndan kaynaklanmaktad\u0131r.<\/p>\n<p style=\"text-align: left;\">Asl\u0131nda mant\u0131k \u00e7er\u00e7evesinde d\u00fc\u015f\u00fcn\u00fcrsek elimizdeki veri k\u00fcmesi LIFO(Last In, First Out &#8211; Son Giren, \u0130lk \u00c7\u0131kar) mant\u0131\u011f\u0131yla \u00e7al\u0131\u015fan Stack ya da\u00a0FIFO(First In, First Out &#8211; \u0130lk Giren, \u0130lk \u00c7\u0131kar) mant\u0131\u011f\u0131yla \u00e7al\u0131\u015fan\u00a0Queue yap\u0131lar\u0131ndan biri yahut benzerleri olabilir. Eee haliyle bu tarz yap\u0131larda sonuncu eleman kavram\u0131,\u00a0LastOrDefault metodundakiyle ayn\u0131 anlam ifade etmiyor olsa gerek.<\/p>\n<p style=\"text-align: left;\">\u0130\u015fte bu a\u00e7\u0131dan olsa gerek\u00a0LastOrDefault metodu i\u015flevselli\u011fini Generic Koleksiyon veya Array yap\u0131s\u0131nda veri k\u00fcmelerine k\u0131s\u0131tlam\u0131\u015f olabilir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class Program\r\n    {\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            NorthwindEntities Veri = new NorthwindEntities();\r\n            Personeller SonPersonel = Veri.Personeller.LastOrDefault();\r\n            Console.WriteLine($&quot;{SonPersonel.Adi} {SonPersonel.SoyAdi}&quot;);\r\n            Console.Read();\r\n        }\r\n    }\r\n<\/pre>\n<p>Yukar\u0131daki \u00f6rnek kod blo\u011funu incelerseniz e\u011fer Entity Framework ile Northwind veritaban\u0131 \u00fczerinde bir \u00e7al\u0131\u015fma ger\u00e7ekle\u015ftirilmi\u015ftir. \u0130\u015fte bu \u00e7al\u0131\u015fma neticesinde ilgili hata ger\u00e7ekle\u015fecektir. Sorunun \u00e7\u00f6z\u00fcm\u00fcn\u00fc a\u015fa\u011f\u0131daki kod blo\u011funda oldu\u011fu gibi sa\u011flayabiliriz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class Program\r\n    {\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            NorthwindEntities Veri = new NorthwindEntities();\r\n            Personeller SonPersonel = Veri.Personeller.ToList().LastOrDefault();\r\n            Console.WriteLine($&quot;{SonPersonel.Adi} {SonPersonel.SoyAdi}&quot;);\r\n            Console.Read();\r\n        }\r\n    }\r\n<\/pre>\n<p>LastOrDefault metodunu, elimizdeki veri k\u00fcmesini ToList metoduyla List koleksiyonuna \u00e7evirdikten sonra hatas\u0131z bir \u015fekilde kullanabilmekteyiz.<\/p>\n<p>Tabi ilgili noktada ToArray metodunuda kullanabilirsiniz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n.\r\n            Personeller SonPersonel = Veri.Personeller.ToArray().LastOrDefault();\r\n.\r\n.\r\n.\r\n<\/pre>\n<p>\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, LINQ to Entities sorgular\u0131ndan LastOrDefault metodu ile elinizdeki veri k\u00fcmesinin\u00a0son eleman\u0131n\u0131 elde etmek istiyor lakin a\u015fa\u011f\u0131daki hatay\u0131 al\u0131yorsan\u0131z \u00e7\u00f6z\u00fcm\u00fc bu i\u00e7eri\u011fimizdedir. An unhandled exception of type &#8216;System.NotSupportedException&#8217; occurred in EntityFramework.SqlServer.dll Additional information: LINQ&#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":3014,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,338,720,955,226],"tags":[260,849,484,1001,1069,1068,1071,1070],"class_list":["post-4024","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-sharp-c","category-c-5-0","category-c-6-0","category-entity-framework","category-linq","tag-hata","tag-hata-cozumleri","tag-hata-cozumu","tag-hatasi-ve-cozumu","tag-lastordefault","tag-linq-to-entities","tag-notsupportedexception","tag-notsupportedexception-hatasi"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/4024","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=4024"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/4024\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/3014"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=4024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=4024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=4024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}