<?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>Wed, 08 Feb 2012 17:49:18 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<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>
	<item>
		<title>By: Gregg</title>
		<link>http://equivalence.co.uk/archives/819/comment-page-1#comment-387</link>
		<dc:creator>Gregg</dc:creator>
		<pubDate>Thu, 12 Mar 2009 21:12:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.equivalence.co.uk/?p=819#comment-387</guid>
		<description>Hi Aerusen. Thanks for commenting. I&#039;m not sure if there is a simpler way to do it - I&#039;m still at the newbie stage with all this C# stuff. If I&#039;m getting what you are saying correct I take it you are essentially trying to perform what CONVERT does in an SQL statement in code? Or am I pick you up wrong?</description>
		<content:encoded><![CDATA[<p>Hi Aerusen. Thanks for commenting. I&#8217;m not sure if there is a simpler way to do it &#8211; I&#8217;m still at the newbie stage with all this C# stuff. If I&#8217;m getting what you are saying correct I take it you are essentially trying to perform what CONVERT does in an SQL statement in code? Or am I pick you up wrong?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aerusen</title>
		<link>http://equivalence.co.uk/archives/819/comment-page-1#comment-385</link>
		<dc:creator>Aerusen</dc:creator>
		<pubDate>Thu, 12 Mar 2009 14:49:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.equivalence.co.uk/?p=819#comment-385</guid>
		<description>How about this?: (obviously needs a try catch around the conversion - will add later)

object valueCast;           
Expression prop = Expression.Property(param, property);
valueCast = Convert.ChangeType(value,Type.GetType(prop.Type.FullName));
Expression val = Expression.Constant(valueCast);</description>
		<content:encoded><![CDATA[<p>How about this?: (obviously needs a try catch around the conversion &#8211; will add later)</p>
<p>object valueCast;<br />
Expression prop = Expression.Property(param, property);<br />
valueCast = Convert.ChangeType(value,Type.GetType(prop.Type.FullName));<br />
Expression val = Expression.Constant(valueCast);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aerusen</title>
		<link>http://equivalence.co.uk/archives/819/comment-page-1#comment-383</link>
		<dc:creator>Aerusen</dc:creator>
		<pubDate>Thu, 12 Mar 2009 14:18:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.equivalence.co.uk/?p=819#comment-383</guid>
		<description>Thanks for this post - i am using the ideas you have to create a dynamic filter on my web site. I am currently trying to add dynamic casting of value types based upon the property type - so i can pass in integers via a string &#039;value&#039; parameter and cast as int if the property is an integer etc... although now i type that there may be a far simpler way to do it. I&#039;ll post it if i figure it out!</description>
		<content:encoded><![CDATA[<p>Thanks for this post &#8211; i am using the ideas you have to create a dynamic filter on my web site. I am currently trying to add dynamic casting of value types based upon the property type &#8211; so i can pass in integers via a string &#8216;value&#8217; parameter and cast as int if the property is an integer etc&#8230; although now i type that there may be a far simpler way to do it. I&#8217;ll post it if i figure it out!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

