{"id":4423,"date":"2008-05-07T19:54:00","date_gmt":"2008-05-07T19:54:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2008\/05\/07\/converting-sql-to-linq-part-10-like-bill-horst\/"},"modified":"2024-07-05T14:25:38","modified_gmt":"2024-07-05T21:25:38","slug":"converting-sql-to-linq-part-10-like-bill-horst","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/converting-sql-to-linq-part-10-like-bill-horst\/","title":{"rendered":"Converting SQL to LINQ, Part 10: Like (Bill Horst)"},"content":{"rendered":"<p class=\"MsoNormal\"><span>This post assumes you&rsquo;ve read my previous <a class=\"\" title=\"Converting SQL to LINQ\" href=\"http:\/\/blogs.msdn.com\/vbteam\/archive\/tags\/Converting+SQL+to+LINQ\/default.aspx\">Converting SQL to LINQ<\/a> posts.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>I haven&rsquo;t had much in the way of specific requests for more LINQ posts, so I&rsquo;d still welcome any suggestions that people want to offer.<span>&nbsp; <\/span>I did get one request about how to mimic the LIKE keyword functionality in VB LINQ.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>In SQL, the LIKE keyword compares a string field against a pattern and returns a Boolean value representing whether the string matches the pattern.<span>&nbsp; <\/span>For example, if I wanted to select every customer in the 206 area code, I could use this SQL query:<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<table class=\"MsoTableGrid\" cellspacing=\"0\" cellpadding=\"0\" border=\"1\">\n<tbody>\n<tr>\n<td class=\"\" valign=\"top\" width=\"638\">\n<p class=\"MsoNoSpacing\"><b><span>SQL<\/p>\n<p><\/span><\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"\" valign=\"top\" width=\"638\">\n<p class=\"MsoNoSpacing\"><span>SELECT *<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>FROM CustomerTable<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>WHERE Phone LIKE &lsquo;206*&rsquo;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>The LIKE expression can include certain wildcard characters, which may differ from system to system.<span>&nbsp; <\/span>In the above example, * matches any string, so &lsquo;206*&rsquo; represents any string that begins with 206.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>VB happens to include a Like operator, so this is an easy conversion to make.<span>&nbsp; <\/span>The LINQ version of this above SQL expression is specified below:<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<table class=\"MsoTableGrid\" cellspacing=\"0\" cellpadding=\"0\" border=\"1\">\n<tbody>\n<tr>\n<td class=\"\" valign=\"top\" width=\"638\">\n<p class=\"MsoNoSpacing\"><b><span>VB<\/p>\n<p><\/span><\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"\" valign=\"top\" width=\"638\">\n<p class=\"MsoNoSpacing\"><span>From Contact In CustomerTable _<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>Where Contact.Phone Like &ldquo;206*&rdquo;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>The VB Like keyword has the following wildcards:<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>?<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Matches any single character<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>*<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Matches zero or more characters<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>#<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Matches any single digit<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>[ <i>charlist<\/i> ]<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Matches any single character in the <i>charlist<\/i><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>[! <i>charlist<\/i> ]<span>&nbsp;&nbsp;&nbsp; <\/span>Matches any single character not in the <i>charlist<\/p>\n<p><\/i><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>More extensive documentation of the Like operator can be found <a class=\"\" title=\"here\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/swf8kaxw(VS.80).aspx\">here<\/a><strong>.<\/p>\n<p><\/strong><\/span><\/p>\n<p class=\"MsoNormal\"><b><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/b><\/p>\n<p class=\"MsoListParagraph\"><span><span>&#8211;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><span>Bill Horst, VB IDE Test<\/span><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post assumes you&rsquo;ve read my previous Converting SQL to LINQ posts. &nbsp; I haven&rsquo;t had much in the way of specific requests for more LINQ posts, so I&rsquo;d still welcome any suggestions that people want to offer.&nbsp; I did get one request about how to mimic the LIKE keyword functionality in VB LINQ. &nbsp; [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[20,195],"tags":[41,49,94,166],"class_list":["post-4423","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-converting-sql-to-linq","category-visual-basic","tag-bill-horst","tag-converting-sql-to-linq","tag-linqvb9","tag-vb2008"],"acf":[],"blog_post_summary":"<p>This post assumes you&rsquo;ve read my previous Converting SQL to LINQ posts. &nbsp; I haven&rsquo;t had much in the way of specific requests for more LINQ posts, so I&rsquo;d still welcome any suggestions that people want to offer.&nbsp; I did get one request about how to mimic the LIKE keyword functionality in VB LINQ. &nbsp; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/4423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=4423"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/4423\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=4423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=4423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=4423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}