﻿
{"id":6540,"date":"2017-06-17T01:29:16","date_gmt":"2017-06-17T01:29:16","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=6540"},"modified":"2017-06-17T01:29:16","modified_gmt":"2017-06-17T01:29:16","slug":"linq-to-entities-does-not-recognize-the-method-int32-toint32system-object-method-and-this-method-cannot-be-translated-unto-a-store-expression-hatasi-ve-cozumu","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/linq-to-entities-does-not-recognize-the-method-int32-toint32system-object-method-and-this-method-cannot-be-translated-unto-a-store-expression-hatasi-ve-cozumu\/","title":{"rendered":"&#8216;LINQ to Entities Does Not Recognize The Method &#8216;Int32 ToInt32(System.Object)&#8217; Method, And This Method Cannot Be Translated Unto A Store Expression.&#8217; Hatas\u0131 ve \u00c7\u00f6z\u00fcm\u00fc"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Projelerimizde kulland\u0131\u011f\u0131m\u0131z ve bizlere Entity Data Modeller \u00fczerinde h\u0131zl\u0131 bir \u015fekilde sorgulama imkan\u0131 sa\u011flayan LINQ sorgular\u0131 \u00fczerinde ba\u015fl\u0131kta da g\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi a\u015fa\u011f\u0131daki olas\u0131 hatay\u0131 alabilirsiniz.<\/p>\n<blockquote>\n<p style=\"text-align: center;\"><span style=\"color: #ff0000;\"><em><strong>System.NotSupportedException: &#8216;LINQ to Entities does not recognize the method &#8216;Int32 ToInt32(System.Object)&#8217; method, and this method cannot be translated into a store expression.&#8217;<\/strong><\/em><\/span><\/p>\n<\/blockquote>\n<p>Hatam\u0131z\u0131n g\u00f6rsel hali ise a\u015fa\u011f\u0131daki gibidir;<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6541\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc.png\" alt=\"'LINQ to Entities Does Not Recognize The Method 'Int32 ToInt32(System.Object)' Method, And This Method Cannot Be Translated Unto A Store Expression.' Hatas\u0131 ve \u00c7\u00f6z\u00fcm\u00fc\" width=\"402\" height=\"104\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc.png 402w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-300x78.png 300w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><\/a><br \/>\nEvet&#8230; Kar\u015f\u0131la\u015f\u0131lmas\u0131 muhtemel hatam\u0131z budur. <strong><em>Peki bu hatay\u0131 neden almaktay\u0131z?<\/em><\/strong><br \/>\n\u00d6ncelikle \u015funu belirtmeliyim ki, LINQ teknolojisini kullan\u0131rken sorgu mant\u0131\u011f\u0131n\u0131 g\u00f6z \u00f6n\u00fcne almazsak bu hatayla kar\u015f\u0131la\u015fmam\u0131z olduk\u00e7a y\u00fcksek ihtimaldir. Nihayetinde ben bir \u00e7al\u0131\u015fma esnas\u0131nda bu mant\u0131\u011f\u0131 g\u00f6z ard\u0131 etti\u011fim i\u00e7in bu hataya haddinden fazla ma\u011fruz kal\u0131p uygulama tasar\u0131m\u0131n\u0131 farkl\u0131 bir \u015fekilde in\u015fa etmek durumunda kalm\u0131\u015f bulunmaktay\u0131m.<\/p>\n<p>&#8220;<em>Peki ne hoca bu mant\u0131k?<\/em>&#8221; diye sab\u0131rs\u0131zca sordu\u011funuzu duyar gibiyim. Hadi gelin a\u00e7\u0131klayal\u0131m.<\/p>\n<p>LINQ sorgular\u0131nda yazm\u0131\u015f oldu\u011fumuz kal\u0131p veritaban\u0131nda \u00e7al\u0131\u015ft\u0131r\u0131lacak sorguyu haz\u0131rlamaktad\u0131rlar. Eee haliyle ilgili kal\u0131b\u0131 yazmak demek; sorguyu olu\u015fturup, veritaban\u0131nda \u00e7al\u0131\u015ft\u0131r\u0131p ard\u0131ndan sonucu getirir manas\u0131na gelmemektedir.<\/p>\n<p>\u015eimdi a\u015fa\u011f\u0131daki kod blo\u011funu ele alal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            Northwind2Entities db = new Northwind2Entities();\r\n\r\n            var Personeller = from Personel in db.Personeller\r\n                              select Personel;\r\n<\/pre>\n<p>Bu kod blo\u011fundaki kal\u0131p, ilgili sorguyu veritaban\u0131na g\u00f6nderip gelen veri k\u00fcmesini var tipinden Personeller referans\u0131na <del>atamamaktad\u0131r<\/del> ! ! ! \u00c7\u00fcnk\u00fc bu kal\u0131p sadece sorguyu haz\u0131rlamaktad\u0131r. Yap\u0131lan LINQ sorgusunun veritaban\u0131nda execute edilmesi i\u00e7in herhangi bir yap\u0131 taraf\u0131ndan \u00e7a\u011f\u0131r\u0131lmas\u0131 gerekmektedir. \u00d6rne\u011fin bu yap\u0131 foreach d\u00f6ng\u00fcs\u00fc olabilir;<\/p>\n<table style=\"width:100%;\">\n<tr>\n<td>\n\u00c7a\u011fr\u0131lmadan \u00d6nce\n<\/td>\n<td>\n\u00c7a\u011fr\u0131ld\u0131ktan Sonra\n<\/td>\n<\/tr>\n<tr>\n<td>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-1.png\" alt=\"&#039;LINQ to Entities Does Not Recognize The Method &#039;Int32 ToInt32(System.Object)&#039; Method, And This Method Cannot Be Translated Unto A Store Expression.&#039; Hatas\u0131 ve \u00c7\u00f6z\u00fcm\u00fc\" width=\"919\" height=\"67\" class=\"aligncenter size-full wp-image-6545\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-1.png 919w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-1-300x22.png 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-1-768x56.png 768w\" sizes=\"auto, (max-width: 919px) 100vw, 919px\" \/><\/a>\n<\/td>\n<td>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-2.png\" alt=\"&#039;LINQ to Entities Does Not Recognize The Method &#039;Int32 ToInt32(System.Object)&#039; Method, And This Method Cannot Be Translated Unto A Store Expression.&#039; Hatas\u0131 ve \u00c7\u00f6z\u00fcm\u00fc\" width=\"849\" height=\"150\" class=\"aligncenter size-full wp-image-6546\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-2.png 849w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-2-300x53.png 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-2-768x136.png 768w\" sizes=\"auto, (max-width: 849px) 100vw, 849px\" \/><\/a>\n<\/td>\n<\/tr>\n<\/table>\n<p>Dikkat ederseniz LINQ sorgumuz foreach taraf\u0131ndan \u00e7a\u011fr\u0131ld\u0131\u011f\u0131 zaman sorgu haline getirilip veritaban\u0131nda execute edilmi\u015ftir. <\/p>\n<p>foreach d\u00f6ng\u00fcs\u00fcnden ziyade LINQ sorgusu neticesinde .ToList() metodu \u00e7a\u011fr\u0131l\u0131rsa e\u011fer tekrardan ilgili ifade veritaban\u0131nda execute edilecektir.<\/p>\n<p>Bizler LINQ sorgular\u0131n\u0131n iterasyonel ya da fonksiyonel \u00e7a\u011fr\u0131m sonucu \u00e7al\u0131\u015ft\u0131r\u0131lmalar\u0131na <em>Ertelenmi\u015f \u00c7al\u0131\u015fma(Deferred Execution)<\/em> demekteyiz.<\/p>\n<p>\u0130\u015fte&#8230; LINQ sorgular\u0131n\u0131 olu\u015ftururken g\u00f6z \u00f6n\u00fcne almam\u0131z gereken yap\u0131sal mant\u0131k bu Deferred Execution olay\u0131d\u0131r.<\/p>\n<p>Konumuz olan &#8220;<em>LINQ to Entities does not recognize the method &#8216;Int32 ToInt32(System.Object)&#8217; method, and this method cannot be translated into a store expression.<\/em>&#8221; hatas\u0131 ise bu bahsetti\u011fim LINQ sorgu mekanizmas\u0131na ra\u011fmen a\u015fa\u011f\u0131daki gibi bir kullan\u0131m sergilemekten kaynaklanmaktad\u0131r.<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-3.png\" alt=\"&#039;LINQ to Entities Does Not Recognize The Method &#039;Int32 ToInt32(System.Object)&#039; Method, And This Method Cannot Be Translated Unto A Store Expression.&#039; Hatas\u0131 ve \u00c7\u00f6z\u00fcm\u00fc\" width=\"594\" height=\"244\" class=\"aligncenter size-full wp-image-6551\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-3.png 594w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-3-300x123.png 300w\" sizes=\"auto, (max-width: 594px) 100vw, 594px\" \/><\/a><\/p>\n<p>G\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere .ToList() metodu \u00e7a\u011fr\u0131lana kadar ilgili LINQ sorgusu b\u00fcnyesinde bir Convert i\u015flemi yap\u0131lmaya \u00e7al\u0131\u015f\u0131lm\u0131\u015ft\u0131r. Eee haliyle compiler elde bir sorgu olmad\u0131\u011f\u0131 i\u00e7in Convert.ToInt32 metodunun T-SQL kar\u015f\u0131l\u0131\u011f\u0131n\u0131 bulamayaca\u011f\u0131ndan dolay\u0131 bu hatay\u0131 vermektedir. Bu hata a\u015fa\u011f\u0131daki gibi bir manevrayla rahatl\u0131kla a\u015f\u0131labilmektedir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        public ActionResult Index(int? id)\r\n        {\r\n            Northwind2Entities db = new Northwind2Entities();\r\n\r\n            if (id != null)\r\n            {\r\n                int ID = Convert.ToInt32(id);\r\n                var GelenPersonel = (from Personel in db.Personeller\r\n                                    where Personel.PersonelID == ID\r\n                                     select Personel).ToList();\r\n            }\r\n            return View();\r\n        }\r\n<\/pre>\n<p>Yani LINQ sorgusunun i\u00e7erisinde bir Convert i\u015flemi yapmaktansa bu i\u015flemi sorgu d\u0131\u015f\u0131nda ger\u00e7ekle\u015ftirip, ard\u0131ndan elde edilen de\u011feri direkt olarak sorguya eklememiz bu hatadan kurtulman\u0131z\u0131 sa\u011flayacakt\u0131r.<\/p>\n<p>Benzer \u015fekilde a\u015fa\u011f\u0131daki Extension metot yap\u0131lar\u0131 ile sorgulama i\u00e7inde ge\u00e7erli olacakt\u0131r.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-4.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-4.png\" alt=\"&#039;LINQ to Entities Does Not Recognize The Method &#039;Int32 ToInt32(System.Object)&#039; Method, And This Method Cannot Be Translated Unto A Store Expression.&#039; Hatas\u0131 ve \u00c7\u00f6z\u00fcm\u00fc\" width=\"570\" height=\"193\" class=\"aligncenter size-full wp-image-6553\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-4.png 570w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2017\/06\/LINQ-to-Entities-Does-Not-Recognize-The-Method-Int32-ToInt32System.Object-Method-And-This-Method-Cannot-Be-Translated-Unto-A-Store-Expression.-Hatas\u0131-ve-\u00c7\u00f6z\u00fcm\u00fc-4-300x102.png 300w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/a><br \/>\nYapmam\u0131z gereken bir \u00f6nceki \u00f6rnekte verilen \u00e7\u00f6z\u00fcm\u00fc uygulamak, yani sorgu i\u00e7erisindeki Convert i\u015flemini d\u0131\u015far\u0131da halletmek yeterli olacakt\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        public ActionResult Index(int? id)\r\n        {\r\n            Northwind2Entities db = new Northwind2Entities();\r\n            int ID = Convert.ToInt32(id);\r\n            if (id != null)\r\n                db.Personeller.FirstOrDefault(y =&gt; y.PersonelID == ID);\r\n            return View();\r\n        }\r\n<\/pre>\n<p>Sonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#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, Projelerimizde kulland\u0131\u011f\u0131m\u0131z ve bizlere Entity Data Modeller \u00fczerinde h\u0131zl\u0131 bir \u015fekilde sorgulama imkan\u0131 sa\u011flayan LINQ sorgular\u0131 \u00fczerinde ba\u015fl\u0131kta da g\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi a\u015fa\u011f\u0131daki olas\u0131 hatay\u0131 alabilirsiniz. System.NotSupportedException: &#8216;LINQ to Entities does not recognize the&#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,226],"tags":[1849,1847,1848,260,484,1083],"class_list":["post-6540","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-sharp-c","category-linq","tag-deferred-execution","tag-entity-data-model","tag-ertelenmis-calisma","tag-hata","tag-hata-cozumu","tag-hata-ve-cozumu"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/6540","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=6540"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/6540\/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=6540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=6540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=6540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}