﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programming By A Tool &#187; Contains</title>
	<atom:link href="http://byatool.com/tag/contains/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>Come waste time with me, because let's be honest:  We're both pathetic and lonely.</description>
	<lastBuildDate>Tue, 07 Sep 2010 14:23:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Like versus Contains in Linq</title>
		<link>http://byatool.com/net-issues/like-versus-contains/</link>
		<comments>http://byatool.com/net-issues/like-versus-contains/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 15:52:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[.Net Issues]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Contains]]></category>
		<category><![CDATA[Like]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/07/22/like-versus-contains/</guid>
		<description><![CDATA[So have to figure this one out. Say you have: private static Expression&#60;Func&#60;User, Boolean&#62;&#62; WhereLikeFirstNameLastNameUserName(String name) { return currentUser =&#62; SqlMethods.Like(currentUser.UserName, "%" + name + "%") &#124;&#124; SqlMethods.Like(currentUser.FirstName, "%" + name + "%") &#124;&#124; SqlMethods.Like(currentUser.LastName, "%" + name + "%"); } And private static Expression &#60;Func&#60;User, Boolean&#62;&#62; WhereLikeFirstNameLastNameUserNameWithoutLike(String name) { return currentUser =&#62; currentUser.UserName.Contains(name) &#124;&#124; [...]]]></description>
			<content:encoded><![CDATA[<p>So have to figure this one out. Say you have:</p>
<pre><span style="color: #3333ff;">private static</span> <span style="color: #00cccc;">Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">Boolean</span>&gt;&gt; WhereLikeFirstNameLastNameUserName(<span style="color: #00cccc;">String </span>name)
{
 <span style="color: #3333ff;"> return </span>currentUser =&gt; <span style="color: #00cccc;">SqlMethods</span>.Like(currentUser.UserName, <span style="color: #cc0000;">"%"</span> + name + <span style="color: #cc0000;">"%"</span>)
  || <span style="color: #00cccc;">SqlMethods</span>.Like(currentUser.FirstName, <span style="color: #cc0000;">"%"</span> + name + <span style="color: #cc0000;">"%"</span>)
  || <span style="color: #00cccc;">SqlMethods</span>.Like(currentUser.LastName, <span style="color: #cc0000;">"%"</span> + name + <span style="color: #cc0000;">"%"</span>);
}</pre>
<p>And</p>
<pre><span style="color: #3333ff;">private static</span> <span style="color: #00cccc;">Expression </span>&lt;<span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">Boolean</span>&gt;&gt; WhereLikeFirstNameLastNameUserNameWithoutLike(<span style="color: #00cccc;">String </span>name)
{
  <span style="color: #3333ff;">return </span>currentUser =&gt; currentUser.UserName.Contains(name)
  || currentUser.FirstName.Contains(name)
  || currentUser.LastName.Contains(name);
}</pre>
<p>The first one should look familiar, its part of the &#8220;dynamic&#8221; linq stuff I&#8217;ve been posting. Now guess which one gives me this SQL when profiled:</p>
<pre><span style="color: #3333ff;">exec </span><span style="color: #660000;">sp_executesql</span> N
<span style="color: #ff0000;">'SELECT</span>
<span style="color: #ff0000;"> [t0].[UserID],</span>
<span style="color: #ff0000;"> [t0].[FirstName],</span>
<span style="color: #ff0000;"> [t0].[LastName],</span>
<span style="color: #ff0000;"> [t0].[Password],</span>
<span style="color: #ff0000;"> [t0].[UserName],</span>
<span style="color: #ff0000;"> [t0].[UserTypeID]</span>
<span style="color: #ff0000;">FROM</span>
<span style="color: #ff0000;"> [dbo].[User] AS [t0]</span>
<span style="color: #ff0000;">WHERE</span>
<span style="color: #ff0000;"> ([t0].[UserName] LIKE @p0)</span>
<span style="color: #ff0000;">OR</span>
<span style="color: #ff0000;"> ([t0].[FirstName] LIKE @p1)</span>
<span style="color: #ff0000;">OR</span>
<span style="color: #ff0000;"> ([t0].[LastName] LIKE @p2)</span>
<span style="color: #ff0000;">ORDER BY</span>
<span style="color: #ff0000;"> [t0].[UserName]'</span>,

N<span style="color: #ff0000;">'@p0 varchar(3),</span>
<span style="color: #ff0000;"> @p1 nvarchar(3),</span>
<span style="color: #ff0000;"> @p2 nvarchar(3)'</span>,
 @p0=<span style="color: #ff0000;">'%s%'</span>,
 @p1=N<span style="color: #ff0000;">'%s%'</span>,
 @p2=N<span style="color: #ff0000;">'%s%'</span></pre>
<p>If you answered both, you are correct or you looked ahead for the answer and therefore are a tool. Now which do you think that&#8230;</p>
<pre>query.ToList()</pre>
<p>Produces the correct list? If you answered Contains, then you are correct again. If you are a tool, you probably looked ahead again&#8230;</p>
<p>Why is this? I HAVE NO IDEA&#8230; Something I have to look into for sure.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/general-coding/linq-extension-methods-versus-linq-query-language-deathmatch/" title="Linq Extension Methods Versus Linq Query Language&#8230; DEATHMATCH">Linq Extension Methods Versus Linq Query Language&#8230; DEATHMATCH</a></li><li><a href="http://byatool.com/general-coding/uhg-it-wont-end/" title="Uhg It Won&#8217;t End">Uhg It Won&#8217;t End</a></li><li><a href="http://byatool.com/pontification/what-is-readable-addon/" title="What Is Readable Addon">What Is Readable Addon</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/net-issues/like-versus-contains/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
