<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: dynamic queries with LINQ</title>
	<atom:link href="http://equivalence.co.uk/archives/819/feed" rel="self" type="application/rss+xml" />
	<link>http://equivalence.co.uk/archives/819</link>
	<description>Technical Blog for Software Developers</description>
	<lastBuildDate>Sat, 17 Mar 2012 10:28:25 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: DanJosef</title>
		<link>http://equivalence.co.uk/archives/819/comment-page-1#comment-1322</link>
		<dc:creator>DanJosef</dc:creator>
		<pubDate>Sat, 17 Mar 2012 10:28:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.equivalence.co.uk/?p=819#comment-1322</guid>
		<description>Yeah it&#039;s removing the Type parameters for some reason...  

Sorry about that -- feel free to email me at the associated address if it&#039;s not clear.</description>
		<content:encoded><![CDATA[<p>Yeah it&#8217;s removing the Type parameters for some reason&#8230;  </p>
<p>Sorry about that &#8212; feel free to email me at the associated address if it&#8217;s not clear.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DanJosef</title>
		<link>http://equivalence.co.uk/archives/819/comment-page-1#comment-1321</link>
		<dc:creator>DanJosef</dc:creator>
		<pubDate>Sat, 17 Mar 2012 10:26:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.equivalence.co.uk/?p=819#comment-1321</guid>
		<description>Strange...  The code I pasted is missing the Type parameters...  Trying again..

        private static Expression GetEqualsExpr(ParameterExpression param,
                                            String property,
                                            TColumn value)
        {
            Expression prop = Expression.Property(param, property);
            Expression val = Expression.Constant(value);
            return Expression.Equal(prop, val);
        }

        public static Expression&lt;Func&gt; GetWhereAndLambda(Dictionary filters)</description>
		<content:encoded><![CDATA[<p>Strange&#8230;  The code I pasted is missing the Type parameters&#8230;  Trying again..</p>
<p>        private static Expression GetEqualsExpr(ParameterExpression param,<br />
                                            String property,<br />
                                            TColumn value)<br />
        {<br />
            Expression prop = Expression.Property(param, property);<br />
            Expression val = Expression.Constant(value);<br />
            return Expression.Equal(prop, val);<br />
        }</p>
<p>        public static Expression&lt;Func&gt; GetWhereAndLambda(Dictionary filters)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DanJosef</title>
		<link>http://equivalence.co.uk/archives/819/comment-page-1#comment-1320</link>
		<dc:creator>DanJosef</dc:creator>
		<pubDate>Sat, 17 Mar 2012 10:24:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.equivalence.co.uk/?p=819#comment-1320</guid>
		<description>I&#039;ve been looking into this for 3 months and this is the first article that started to make sense.

I think that you can make it even more powerful using Type parameters for the Type of object you are filtering... So, e.g., the GetEquals method becomes something like:


        private Expression GetEqualsExpr(ParameterExpression param,
                                            String property,
                                            TColumn value)
        {
            Expression prop = Expression.Property(param, property);
            Expression val = Expression.Constant(value);
            return Expression.Equal(prop, val);
        }

signature for GetWhereLambda becomes something like:

public static Expression&lt;Func&gt; GetWhereAndLambda

Thank you!</description>
		<content:encoded><![CDATA[<p>I&#8217;ve been looking into this for 3 months and this is the first article that started to make sense.</p>
<p>I think that you can make it even more powerful using Type parameters for the Type of object you are filtering&#8230; So, e.g., the GetEquals method becomes something like:</p>
<p>        private Expression GetEqualsExpr(ParameterExpression param,<br />
                                            String property,<br />
                                            TColumn value)<br />
        {<br />
            Expression prop = Expression.Property(param, property);<br />
            Expression val = Expression.Constant(value);<br />
            return Expression.Equal(prop, val);<br />
        }</p>
<p>signature for GetWhereLambda becomes something like:</p>
<p>public static Expression&lt;Func&gt; GetWhereAndLambda</p>
<p>Thank you!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kenan Bek</title>
		<link>http://equivalence.co.uk/archives/819/comment-page-1#comment-1274</link>
		<dc:creator>Kenan Bek</dc:creator>
		<pubDate>Tue, 11 Oct 2011 04:52:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.equivalence.co.uk/?p=819#comment-1274</guid>
		<description>also you can use CodeDOM and Dynamic Library for creating LINQ queries at runtime. more detailed about this approachs you can read here http://kenanbek.com/site/post/dynamic-linq-queries-build-linq-queries-from-text-at-runtime/.</description>
		<content:encoded><![CDATA[<p>also you can use CodeDOM and Dynamic Library for creating LINQ queries at runtime. more detailed about this approachs you can read here <a href="http://kenanbek.com/site/post/dynamic-linq-queries-build-linq-queries-from-text-at-runtime/" rel="nofollow">http://kenanbek.com/site/post/dynamic-linq-queries-build-linq-queries-from-text-at-runtime/</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: George</title>
		<link>http://equivalence.co.uk/archives/819/comment-page-1#comment-1223</link>
		<dc:creator>George</dc:creator>
		<pubDate>Mon, 18 Oct 2010 15:01:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.equivalence.co.uk/?p=819#comment-1223</guid>
		<description>Why not to use Predicate Builder http://www.albahari.com/nutshell/predicatebuilder.aspx ?</description>
		<content:encoded><![CDATA[<p>Why not to use Predicate Builder <a href="http://www.albahari.com/nutshell/predicatebuilder.aspx" rel="nofollow">http://www.albahari.com/nutshell/predicatebuilder.aspx</a> ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://equivalence.co.uk/archives/819/comment-page-1#comment-1210</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Wed, 09 Jun 2010 16:33:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.equivalence.co.uk/?p=819#comment-1210</guid>
		<description>Hi

I tried your code and figured out that to finally use the expression in the Where-Method I had to call the Compile-Method.

The final line should therefore look like this:

IEnumerable filtered = students.Where(myLambda.Compile());

Hopefully this helps other people facing the same trouble.</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>I tried your code and figured out that to finally use the expression in the Where-Method I had to call the Compile-Method.</p>
<p>The final line should therefore look like this:</p>
<p>IEnumerable filtered = students.Where(myLambda.Compile());</p>
<p>Hopefully this helps other people facing the same trouble.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ferry Wijaya</title>
		<link>http://equivalence.co.uk/archives/819/comment-page-1#comment-445</link>
		<dc:creator>Ferry Wijaya</dc:creator>
		<pubDate>Thu, 14 May 2009 11:41:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.equivalence.co.uk/?p=819#comment-445</guid>
		<description>your function is to check equality of field and value. hm .. how about for checking a series of character in field (contains) statement ?. 

Tuhank&#039;s</description>
		<content:encoded><![CDATA[<p>your function is to check equality of field and value. hm .. how about for checking a series of character in field (contains) statement ?. </p>
<p>Tuhank&#8217;s</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charley</title>
		<link>http://equivalence.co.uk/archives/819/comment-page-1#comment-442</link>
		<dc:creator>Charley</dc:creator>
		<pubDate>Tue, 21 Apr 2009 15:08:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.equivalence.co.uk/?p=819#comment-442</guid>
		<description>Hi - I am trying to create a dynamic query with an arbitrary number of OR conditions, and I am getting really confused.

My user is going to hand me an ArrayList (let&#039;s call it input) with tags/categories, along with an integer for the ID

so I get values of input[0]=&quot;foo&quot;, input[1]=&quot;bar&quot;, input[2]=&quot;baz&quot;

I want to construct a query that does:

query = (from c in db.theTable
where c.ID == inputID
&amp;&amp; c.tags == 

//This is where I run into trouble - I want to loop through the //arraylist with a foreach (or for or whatever) and construct a series //of OR conditions to put into the query here) - I need to be able to //have an arbitrary number of conditions

select c);</description>
		<content:encoded><![CDATA[<p>Hi &#8211; I am trying to create a dynamic query with an arbitrary number of OR conditions, and I am getting really confused.</p>
<p>My user is going to hand me an ArrayList (let&#8217;s call it input) with tags/categories, along with an integer for the ID</p>
<p>so I get values of input[0]=&#8221;foo&#8221;, input[1]=&#8221;bar&#8221;, input[2]=&#8221;baz&#8221;</p>
<p>I want to construct a query that does:</p>
<p>query = (from c in db.theTable<br />
where c.ID == inputID<br />
&amp;&amp; c.tags == </p>
<p>//This is where I run into trouble &#8211; I want to loop through the //arraylist with a foreach (or for or whatever) and construct a series //of OR conditions to put into the query here) &#8211; I need to be able to //have an arbitrary number of conditions</p>
<p>select c);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gregg</title>
		<link>http://equivalence.co.uk/archives/819/comment-page-1#comment-389</link>
		<dc:creator>Gregg</dc:creator>
		<pubDate>Thu, 12 Mar 2009 22:43:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.equivalence.co.uk/?p=819#comment-389</guid>
		<description>Ah right. I was a little slow to catch on to what you were doing - for some reason I was thinking you were doing this for the Select method rather than the Where (probably because I was looking at something similar earlier). So yeah, what you have done seems good, I would be surprised if there was another, simpler, way to achieve this. Hopefully others that visit with similar problems can make use of this. Good stuff, thanks.</description>
		<content:encoded><![CDATA[<p>Ah right. I was a little slow to catch on to what you were doing &#8211; for some reason I was thinking you were doing this for the Select method rather than the Where (probably because I was looking at something similar earlier). So yeah, what you have done seems good, I would be surprised if there was another, simpler, way to achieve this. Hopefully others that visit with similar problems can make use of this. Good stuff, thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aerusen</title>
		<link>http://equivalence.co.uk/archives/819/comment-page-1#comment-388</link>
		<dc:creator>Aerusen</dc:creator>
		<pubDate>Thu, 12 Mar 2009 21:34:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.equivalence.co.uk/?p=819#comment-388</guid>
		<description>No that&#039;s right - in my code i am taking field names and values from a web page so everything is hitting the code as strings - and i want to be able to use your code to loop through each filed they are trying to filter with - but it fails if i try and pass a string representaion of (for example) &quot;4&quot; into an expression where the base field is an integer. It is nice that &#039;Expression&#039; lets you then query what the DB field type is.

The code I added to your code basically lets you pass in any field and value and it will sort out the type matching at run time - so we can pass the following and it be ok:

[Field name (type) -- value]
FirstName (varchar(50)) -- &quot;Amye&quot;
LastName (varchar(50)) -- &quot;Couves&quot;
Age (Int32) -- &quot;30&quot; (note here that this would normally cause the code to fail but the ChangeType function changes &quot;30&quot; to 30 at runtime.

As I said - i think some try {} catch {} is needed around the convert as it would throw an exception on converting &quot;bob&quot; to an integer (the user might put the wrong data in a search field by mistake) but this is easy to add and can then be passed back to the front end as an error.

Thanks again!</description>
		<content:encoded><![CDATA[<p>No that&#8217;s right &#8211; in my code i am taking field names and values from a web page so everything is hitting the code as strings &#8211; and i want to be able to use your code to loop through each filed they are trying to filter with &#8211; but it fails if i try and pass a string representaion of (for example) &#8220;4&#8243; into an expression where the base field is an integer. It is nice that &#8216;Expression&#8217; lets you then query what the DB field type is.</p>
<p>The code I added to your code basically lets you pass in any field and value and it will sort out the type matching at run time &#8211; so we can pass the following and it be ok:</p>
<p>[Field name (type) -- value]<br />
FirstName (varchar(50)) &#8212; &#8220;Amye&#8221;<br />
LastName (varchar(50)) &#8212; &#8220;Couves&#8221;<br />
Age (Int32) &#8212; &#8220;30&#8243; (note here that this would normally cause the code to fail but the ChangeType function changes &#8220;30&#8243; to 30 at runtime.</p>
<p>As I said &#8211; i think some try {} catch {} is needed around the convert as it would throw an exception on converting &#8220;bob&#8221; to an integer (the user might put the wrong data in a search field by mistake) but this is easy to add and can then be passed back to the front end as an error.</p>
<p>Thanks again!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

