{"id":645,"date":"2009-05-25T14:29:00","date_gmt":"2009-05-25T14:29:00","guid":{"rendered":"\/blogs\/bobb\/post\/L2S-and-EF-parameterization-problem-fixed-in-NET-40-almost.aspx"},"modified":"2013-01-03T23:59:45","modified_gmt":"2013-01-04T07:59:45","slug":"l2s-and-ef-parameterization-problem-fixed-in-net-4-0-almost","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/l2s-and-ef-parameterization-problem-fixed-in-net-4-0-almost\/","title":{"rendered":"L2S and EF parameterization problem fixed in .NET 4.0 &#8211; almost"},"content":{"rendered":"<p>\nOne of the first things that I did after installing VS2010 beta this week was to check on a parameterization problem with string parameters, and Linq To SQL\/ADO.NET Entity Framework generated code. In the original version, this L2S query:\n<\/p>\n<p>\nvar x = from a in ents.authors<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where a.au_lname == &quot;Smith&quot;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select new { a.au_lname, a.au_fname };\n<\/p>\n<p>\nor this EF query:\n<\/p>\n<p>\nstring name = &quot;Smith&quot;;<br \/>\nvar x = from a in ents.authors<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where a.au_lname == name<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select new { a.au_lname, a.au_fname };\n<\/p>\n<p>\nWould cause plan cache pollution and possibly performance problems. These queries produced a query plan with the parameter&nbsp;specified as NVARCHAR(5). Each query with a different length will produce a new plan in the cache, and the use of NVARCHAR rather than VARCHAR will cause a table scan (rather than a seek) in the plan, regardless of how many rows are in the table, because there must be a conversion of au_lname from VARCHAR to NVARCHAR as part getting the rows, making the query non-sargable. The correct database data type (VARCHAR) needs to be specified as the parameter data type.\n<\/p>\n<p>\nIn .NET 4.0\/VS2010 Beta 1, L2S handles this, producing a VARCHAR(8000) parameter, as SQL Server&#39;s autoparameterization does. Entity Framework produces NVARCHAR(4000), fixing the length problem but still using the a different data type than the table specifies.\n<\/p>\n<p>\nNOTE: This <strong>will<\/strong> be fixed in the next beta version of .NET 4.0 to produce VARCHAR(8000). See complete post <a href=\"http:\/\/3.209.169.194\/blogs\/bobb\/l2s-and-ef-parameterization-problem-will-be-fixed-in-net-4-0-completely\/\">here<\/a>.\n<\/p>\n<p>\nIn my next installment of my MSDN &quot;Under The Table&quot;, I&#39;ll be writing about the performance implications of data access method code in detail. I do mention &quot;fixed in the next release&quot; WRT L2S and EF, and hope that it will be resolved before .NET 4.0 ships.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the first things that I did after installing VS2010 beta this week was to check on a parameterization problem with string parameters, and Linq To SQL\/ADO.NET Entity Framework generated code. In the original version, this L2S query: var x = from a in ents.authors &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where a.au_lname == &quot;Smith&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select new { [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-645","post","type-post","status-publish","format-standard","hentry","category-data-access"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>L2S and EF parameterization problem fixed in .NET 4.0 - almost - Bob Beauchemin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/l2s-and-ef-parameterization-problem-fixed-in-net-4-0-almost\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"L2S and EF parameterization problem fixed in .NET 4.0 - almost - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"One of the first things that I did after installing VS2010 beta this week was to check on a parameterization problem with string parameters, and Linq To SQL\/ADO.NET Entity Framework generated code. In the original version, this L2S query: var x = from a in ents.authors &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where a.au_lname == &quot;Smith&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select new { [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/l2s-and-ef-parameterization-problem-fixed-in-net-4-0-almost\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2009-05-25T14:29:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-01-04T07:59:45+00:00\" \/>\n<meta name=\"author\" content=\"Bob Beauchemin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Beauchemin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/l2s-and-ef-parameterization-problem-fixed-in-net-4-0-almost\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/l2s-and-ef-parameterization-problem-fixed-in-net-4-0-almost\/\",\"name\":\"L2S and EF parameterization problem fixed in .NET 4.0 - almost - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2009-05-25T14:29:00+00:00\",\"dateModified\":\"2013-01-04T07:59:45+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/l2s-and-ef-parameterization-problem-fixed-in-net-4-0-almost\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/l2s-and-ef-parameterization-problem-fixed-in-net-4-0-almost\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/l2s-and-ef-parameterization-problem-fixed-in-net-4-0-almost\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Data Access\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/data-access\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"L2S and EF parameterization problem fixed in .NET 4.0 &#8211; almost\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\",\"name\":\"Bob Beauchemin\",\"description\":\"SQL Server Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\",\"name\":\"Bob Beauchemin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"caption\":\"Bob Beauchemin\"},\"sameAs\":[\"http:\/www.sqlskills.com\/blogs\/bobb\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"L2S and EF parameterization problem fixed in .NET 4.0 - almost - Bob Beauchemin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/bobb\/l2s-and-ef-parameterization-problem-fixed-in-net-4-0-almost\/","og_locale":"en_US","og_type":"article","og_title":"L2S and EF parameterization problem fixed in .NET 4.0 - almost - Bob Beauchemin","og_description":"One of the first things that I did after installing VS2010 beta this week was to check on a parameterization problem with string parameters, and Linq To SQL\/ADO.NET Entity Framework generated code. In the original version, this L2S query: var x = from a in ents.authors &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where a.au_lname == &quot;Smith&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select new { [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/l2s-and-ef-parameterization-problem-fixed-in-net-4-0-almost\/","og_site_name":"Bob Beauchemin","article_published_time":"2009-05-25T14:29:00+00:00","article_modified_time":"2013-01-04T07:59:45+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/l2s-and-ef-parameterization-problem-fixed-in-net-4-0-almost\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/l2s-and-ef-parameterization-problem-fixed-in-net-4-0-almost\/","name":"L2S and EF parameterization problem fixed in .NET 4.0 - almost - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2009-05-25T14:29:00+00:00","dateModified":"2013-01-04T07:59:45+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/l2s-and-ef-parameterization-problem-fixed-in-net-4-0-almost\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/l2s-and-ef-parameterization-problem-fixed-in-net-4-0-almost\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/l2s-and-ef-parameterization-problem-fixed-in-net-4-0-almost\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"Data Access","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/data-access\/"},{"@type":"ListItem","position":3,"name":"L2S and EF parameterization problem fixed in .NET 4.0 &#8211; almost"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/","name":"Bob Beauchemin","description":"SQL Server Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e","name":"Bob Beauchemin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","caption":"Bob Beauchemin"},"sameAs":["http:\/www.sqlskills.com\/blogs\/bobb\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/comments?post=645"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/645\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}