{"id":39,"date":"2011-10-21T14:09:00","date_gmt":"2011-10-21T14:09:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2011\/10\/21\/linqtosql-count\/"},"modified":"2011-10-21T14:09:00","modified_gmt":"2011-10-21T14:09:00","slug":"linqtosql-count","status":"publish","type":"post","link":"http:\/\/panahy.nl\/index.php\/2011\/10\/21\/linqtosql-count\/","title":{"rendered":"LinqToSql Count"},"content":{"rendered":"<p>You can pass the results of a linq query to the next statement like this one<\/p>\n<div><\/p>\n<div><span style=\"font-family: 'Courier New', Courier, monospace;\">var q = context.Products<\/span><\/div>\n<div><span style=\"font-family: 'Courier New', Courier, monospace;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(p =&gt; p.ProductName.StartsWith(&#8220;A&#8221;))<\/span><\/div>\n<div><span style=\"font-family: 'Courier New', Courier, monospace;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Count();<\/span><\/div>\n<\/div>\n<div><\/div>\n<div>and that should provide the count of products with names starting with &#8220;A&#8221;.&nbsp;<\/div>\n<div>Further it would make no difference (performance wise) to skip theWhere statement and simply call the Count with filter as parameter, like:<\/div>\n<div><\/div>\n<div>\n<div><span style=\"font-family: 'Courier New', Courier, monospace;\">var q = context.Products<\/span><\/div>\n<div><span style=\"font-family: 'Courier New', Courier, monospace;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Count(p =&gt; p.ProductName.StartsWith(&#8220;B&#8221;));<\/span><\/div>\n<\/div>\n<div><\/div>\n<div>or in another syntax like:<\/div>\n<div><\/div>\n<div>\n<div><span style=\"font-family: 'Courier New', Courier, monospace;\">&nbsp; &nbsp; q = (from p in context.Products<\/span><\/div>\n<div><span style=\"font-family: 'Courier New', Courier, monospace;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;where p.ProductName.StartsWith(&#8220;C&#8221;)<\/span><\/div>\n<div><span style=\"font-family: 'Courier New', Courier, monospace;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select true).Count();<\/span><\/div>\n<\/div>\n<div><\/div>\n<div>So, I did put a tracing into the Context.Log to see what it goes to the SQL-Server. I&#8217;ve got the following for all three cases:<\/div>\n<div><\/div>\n<div>\n<div><span style=\"font-family: 'Courier New', Courier, monospace;\">SELECT COUNT(*) AS [value]<\/span><\/div>\n<div><span style=\"font-family: 'Courier New', Courier, monospace;\">FROM [dbo].[Products] AS [t0]<\/span><\/div>\n<div><span style=\"font-family: 'Courier New', Courier, monospace;\">WHERE [t0].[ProductName] LIKE @p0<\/span><\/div>\n<div><b><span style=\"font-family: 'Courier New', Courier, monospace;\">&#8212; @p0: Input NVarChar (Size = 4000; Prec = 0; Scale = 0) [A%]<\/span><\/b><\/div>\n<div><span style=\"font-family: 'Courier New', Courier, monospace;\">&#8212; Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1<\/span><\/div>\n<div><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>You can pass the results of a linq query to the next statement like this one var q = context.Products &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(p =&gt; p.ProductName.StartsWith(&#8220;A&#8221;)) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Count(); and that should provide the count of products with names starting with &#8220;A&#8221;.&nbsp; Further it &hellip; <a href=\"http:\/\/panahy.nl\/index.php\/2011\/10\/21\/linqtosql-count\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;LinqToSql Count&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[33],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":0,"uagb_excerpt":"You can pass the results of a linq query to the next statement like this one var q = context.Products &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(p =&gt; p.ProductName.StartsWith(&#8220;A&#8221;)) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Count(); and that should provide the count of products with names starting with &#8220;A&#8221;.&nbsp; Further it&hellip;","_links":{"self":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/39"}],"collection":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":0,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"wp:attachment":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}