﻿<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://developers.de/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Andreas Lehmann : LINQ</title><link />http://developers.de/blogs/andreas_lehmann/archive/tags/LINQ/default.aspx<description>Tags: LINQ</description><dc:language>en</dc:language><generator>CommunityServer 2008 SP1 (Build: 30619.63)</generator><item><title>How to use Entity Framework to query string values with filter conditions</title><link />http://developers.de/blogs/andreas_lehmann/archive/2009/04/08/how-to-use-entity-framework-to-query-string-values-with-filter-conditions.aspx<pubdate>Wed, 08 Apr 2009 12:57:33 GMT</pubdate><guid ispermalink="false">7e491611-45ad-4dae-a68f-c4cb64439510:4846</guid><dc:creator>alehmann</dc:creator><slash:comments>1</slash:comments><wfw:commentrss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://developers.de/blogs/andreas_lehmann/rsscomments.aspx?PostID=4846</wfw:commentrss><comments>http://developers.de/blogs/andreas_lehmann/archive/2009/04/08/how-to-use-entity-framework-to-query-string-values-with-filter-conditions.aspx#comments</comments><description>&lt;p&gt;Have you ever tried to filter a LINQ statement by string values. E.g. you want to get all persons where the name starts with letters from ‘A’ to ‘Z’. In this post I will show you how to build a query like this.&lt;/p&gt;  &lt;p&gt;My first approach was to do a simple &amp;lt;= operation like I do in SQL. But this results in an exception: “Operator &amp;#39;&amp;lt;=&amp;#39; cannot be applied to operands of type &amp;#39;string&amp;#39; and &amp;#39;string&amp;#39;”. OK, I remember, we are in the .NET world. So I tried the .CompareTo() method of string. And it seems to work. So my LINQ statement would be:&lt;/p&gt;  &lt;div style="border-bottom:gray 1px solid;border-left:gray 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;max-height:200px;font-size:8pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;padding-top:4px;"&gt;   &lt;div style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;     &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; valueMin = &lt;span style="color:#006080;"&gt;&amp;quot;A&amp;quot;&lt;/span&gt;;&lt;/pre&gt;

    &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;"&gt;   2:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; valueMax = &lt;span style="color:#006080;"&gt;&amp;quot;Z&amp;quot;&lt;/span&gt;;&lt;/pre&gt;

    &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;"&gt;   3:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;"&gt;   4:&lt;/span&gt; var x = from p &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; context.TPerson&lt;/pre&gt;

    &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;"&gt;   5:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; p.Name.CompareTo(valueMin) &amp;gt;= 0 &amp;amp;&amp;amp;&lt;/pre&gt;

    &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;"&gt;   6:&lt;/span&gt;               p.Name.CompareTo(valueMax) &amp;lt;= 0&lt;/pre&gt;

    &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;"&gt;   7:&lt;/span&gt;         select p;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Imagine a list of people: “Anton”, “Bill”, “Hans”, “Hugo”, “Steve” and “Xavier”. When I set the valueMin to ‘A’ and valueMax to ‘Z’ I receive all names. When I set valueMax to ‘X’, “Xavier” is NOT returned. That is because “Xavier”.CompareTo(“X”) returns 1 and 1 is not &amp;lt;= 0. Writing my own string comparer is not possible in this scenario because it would not be used by the Entity Framework. So I have to find a workaround. And here is the LINQ statement that works fine for me:&lt;/p&gt;

&lt;div style="border-bottom:gray 1px solid;border-left:gray 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;max-height:200px;font-size:8pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;padding-top:4px;"&gt;
  &lt;div style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;
    &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; valueMin = &lt;span style="color:#006080;"&gt;&amp;quot;A&amp;quot;&lt;/span&gt;;&lt;/pre&gt;

    &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;"&gt;   2:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; valueMax = &lt;span style="color:#006080;"&gt;&amp;quot;Z&amp;quot;&lt;/span&gt;;&lt;/pre&gt;

    &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;"&gt;   3:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;"&gt;   4:&lt;/span&gt; var x = from p &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; context.TPerson&lt;/pre&gt;

    &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;"&gt;   5:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; p.Name.ToLower().Substring(0, valueMin.Length).CompareTo(valueMin.ToLower()) &amp;gt;= 0 &amp;amp;&amp;amp;&lt;/pre&gt;

    &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;"&gt;   6:&lt;/span&gt;               p.Name.ToLower().Substring(0, valueMax.Length).CompareTo(valueMax.ToLower()) &amp;lt;= 0&lt;/pre&gt;

    &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;"&gt;   7:&lt;/span&gt;         select p;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Remembering the above problem I have to find a way where “Xavier”.CompareTo(“X”) results in –1 or 0 to make my query work. So –1 would be the wrong result, what I really want is that it return 0. CompareTo() will return 0 when both values are equal. So lets make them equal… Therefore all values are converted to lower (or upper) case. Than we shorten the queried value to the length of the filter value. In result we get the following statement: “x”.CompareTo(“x”) which results in 0. So the above query works as expected.&lt;/p&gt;

&lt;p&gt;The query by valueMin = ‘A’ and valueMax = ‘A’ returns all persons where the name starts with ‘A’.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=4846" width="1" height="1"&gt;</description><category domain="http://developers.de/blogs/andreas_lehmann/archive/tags/.NET+3.5/default.aspx">.NET 3.5</category><category domain="http://developers.de/blogs/andreas_lehmann/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://developers.de/blogs/andreas_lehmann/archive/tags/LINQ/default.aspx">LINQ</category></item></channel></rss>