VB.NET LINQ to SQL Dynamic Where Clause and Joinshttp://forums.asp.net/t/1792198.aspx/1?VB+NET+LINQ+to+SQL+Dynamic+Where+Clause+and+JoinsTue, 24 Apr 2012 20:13:57 -040017921984929319http://forums.asp.net/p/1792198/4929319.aspx/1?VB+NET+LINQ+to+SQL+Dynamic+Where+Clause+and+JoinsVB.NET LINQ to SQL Dynamic Where Clause and Joins <p>I'm pretty new to LINQ to SQL and have come across a problem with dynamic Where clauses and Joins. For exampe, if I have 2 parameters, one of them optional, I need to find a way to include/exclude the optional parameter from my query. In SQL I would build up statement as a string and then execute it. I have read an example by ScottGU - http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx - which handles a single table but I can't find a way to do this with multiple joins.</p> <p>Here is the code I am using</p> <p></p> <pre class="prettyprint">Public Function GetCompByFacilityID(ByVal intFacilityID As Integer, ByVal strUsername As String) As Object Dim dc As New DCMainDataContext Dim query1 = Nothing Try query1 = (From cmp In dc.CMP_Comp _ Join fac In dc.Facilities On cmp.FacilityID Equals fac.FacilityID _ Join usrfac In dc.UserFacilities On fac.FacilityID Equals usrfac.FacilityID _ Join cmpfrm In dc.LU_FormOfComp On cmp.FormComplimentID Equals cmpfrm.FormComplimentID _ Where usrfac.Username = strUsername And fac.FacilityID = intFacilityID _ Select cmp.FacilityID, fac.FacilityName, cmp.CompDate, cmpfrm.FormComp, cmp.ReceivedFrom _ Order By FacilityName).ToList Catch ex As Exception Throw ex Finally dc = Nothing End Try Return query1 End Function</pre> <p></p> <p>If the value of intFacility ID is -1 I want to exclude this from the statement altogether. Does anyone have an example of how to do this?<br> <br> </p> 2012-04-12T14:01:30-04:004931929http://forums.asp.net/p/1792198/4931929.aspx/1?Re+VB+NET+LINQ+to+SQL+Dynamic+Where+Clause+and+JoinsRe: VB.NET LINQ to SQL Dynamic Where Clause and Joins <p>Hello</p> <p>As far as I seeyou can use if statement to deal with the problem</p> <pre class="prettyprint">Public Function GetCompByFacilityID(ByVal intFacilityID As Integer, ByVal strUsername As String) As Object Dim dc As New DCMainDataContext Dim query1 = Nothing Try <strong> If(Your Conditional Thing here) Then</strong> <strong> </strong> <strong> End If</strong> query1 = (From cmp In dc.CMP_Comp _ Join fac In dc.Facilities On cmp.FacilityID Equals fac.FacilityID _ Join usrfac In dc.UserFacilities On fac.FacilityID Equals usrfac.FacilityID _ Join cmpfrm In dc.LU_FormOfComp On cmp.FormComplimentID Equals cmpfrm.FormComplimentID _ Where usrfac.Username = strUsername And fac.FacilityID = intFacilityID _ Select cmp.FacilityID, fac.FacilityName, cmp.CompDate, cmpfrm.FormComp, cmp.ReceivedFrom _ Order By FacilityName).ToList Catch ex As Exception Throw ex Finally dc = Nothing End Try Return query1 End Function</pre> 2012-04-14T01:30:32-04:004934028http://forums.asp.net/p/1792198/4934028.aspx/1?Re+VB+NET+LINQ+to+SQL+Dynamic+Where+Clause+and+JoinsRe: VB.NET LINQ to SQL Dynamic Where Clause and Joins <p>Do you mean writing the query out multiple times? So for each condition (optional parameter) I would have the whole query again but with the inclusion of the optional parameter? This is what I'm trying to avoid really. If I have 20 parameters and then want to change part of the query itself it becomes time consuming. Certainly a step back from what is available in SQL. This is what I would do in SQL -</p> <pre class="prettyprint">create proc sp_example @strUsername varchar(50), @intFacility varchar(50), @param3 varchar(50), @param4 varchar(50) as declare @sql varchar(500) set @sql = ' select field1, field2 from tableName where Username = ' &#43; @strUsername if @intFacility &lt;&gt; '-1' set @sql = @sql &#43; ' and Facility = ' &#43; @intFacility if @param3 &lt;&gt; '' set @sql = @sql &#43; ' and field3 = ' &#43; @param3 if @param4 &lt;&gt; '' set @sql = @sql &#43; ' and field4 = ' &#43; @param4 exec(@sql)</pre> <p>Sorry if I've misunderstood what you mean?</p> 2012-04-16T07:50:02-04:004934051http://forums.asp.net/p/1792198/4934051.aspx/1?Re+VB+NET+LINQ+to+SQL+Dynamic+Where+Clause+and+JoinsRe: VB.NET LINQ to SQL Dynamic Where Clause and Joins <p>WellIn my mind I think you need Dynamic LINQ by using CreateQuery&lt;T&gt; methodthis will let you dynamic pass a string as its parameter value to query things outAnd what's more importantCreateQuery&lt;T&gt; 's parameter can be combined by string splits</p> <p>Sample</p> <pre class="prettyprint">using (AdventureWorksEntities context = new AdventureWorksEntities()) { string queryString = @&quot;SELECT VALUE contact FROM AdventureWorksEntities.Contacts AS contact WHERE contact.FirstName = @fn&quot;; ObjectQuery&lt;Contact&gt; contactQuery = context.CreateQuery&lt;Contact&gt;(queryString, new ObjectParameter(&quot;fn&quot;, &quot;Frances&quot;)); // Iterate through the collection of Contact items. foreach (Contact result in contactQuery) Console.WriteLine(&quot;First Name: {0}, Last Name: {1}&quot;, result.FirstName, result.LastName); }</pre> <p></p> 2012-04-16T07:58:00-04:004938148http://forums.asp.net/p/1792198/4938148.aspx/1?Re+VB+NET+LINQ+to+SQL+Dynamic+Where+Clause+and+JoinsRe: VB.NET LINQ to SQL Dynamic Where Clause and Joins <p>Do you have a sample of this in vb.net? I'm not sure how&nbsp;I would use this to select multiple fields from&nbsp;multiple tables?</p> 2012-04-18T08:40:14-04:004938175http://forums.asp.net/p/1792198/4938175.aspx/1?Re+VB+NET+LINQ+to+SQL+Dynamic+Where+Clause+and+JoinsRe: VB.NET LINQ to SQL Dynamic Where Clause and Joins <p></p> <blockquote><span class="icon-blockquote"></span> <h4>blued</h4> <p></p> <p>Do you have a sample of this in vb.net? I'm not sure how&nbsp;I would use this to select multiple fields from&nbsp;multiple tables?</p> <p></p> </blockquote> <p></p> <p>Hello again</p> <p>This is the VB.NET sample</p> <pre class="prettyprint">Using context As New AdventureWorksEntities() Dim queryString As String = &quot;SELECT VALUE contact FROM AdventureWorksEntities.Contacts &quot; &amp; _ &quot; AS contact WHERE contact.FirstName = @fn&quot; Dim contactQuery As ObjectQuery(Of Contact) = context.CreateQuery(Of Contact)(queryString, New ObjectParameter(&quot;fn&quot;, &quot;Frances&quot;)) ' Iterate through the collection of Contact items. For Each result As Contact In contactQuery Console.WriteLine(&quot;First Name: {0}, Last Name: {1}&quot;, result.FirstName, result.LastName) Next End Using</pre> <p>This is only a samplebut what I want to say to you is you can combine strings' splits together with ifstatements and then pass the combined whole string as a value in the CreateQuery's parameter to get what you want-</p> 2012-04-18T08:52:11-04:004938234http://forums.asp.net/p/1792198/4938234.aspx/1?Re+VB+NET+LINQ+to+SQL+Dynamic+Where+Clause+and+JoinsRe: VB.NET LINQ to SQL Dynamic Where Clause and Joins <p>Hi, I appreciate the C# code can be converted to VB, but I thought there might have been something in more detail on MSDN or similar? My searches aren't turning up much and it is how to put together a query that would return the same results as my original query that I'm having trouble with. I understand that you can use if...statements to build up the query but how does</p> <pre class="prettyprint">query = From cmp In dc.CMP_Comp _ Join fac In dc.Facilities On cmp.FacilityID Equals fac.FacilityID _ Join usrfac In dc.UserFacilities On fac.FacilityID Equals usrfac.FacilityID _ Join cmpfrm In dc.LU_FormOfComp On cmp.FormComplimentID Equals cmpfrm.FormComplimentID</pre> <p>become something that fits with</p> <pre class="prettyprint">Dim queryString As String = "SELECT VALUE contact FROM AdventureWorksEntities.Contacts " &amp; _ " AS contact</pre> <p>This looks more like SQL than LINQ. Can I do something like the following?</p> <pre class="prettyprint">Dim queryString As String = "From cmp In dc.CMP_Comp " queryString &amp;= "Join fac In dc.Facilities On cmp.FacilityID Equals fac.FacilityID " queryString &amp;= "Join usrfac In dc.UserFacilities On fac.FacilityID Equals usrfac.FacilityID " queryString &amp;= "Join cmpfrm In dc.LU_FormOfComp On cmp.FormComplimentID Equals cmpfrm.FormComplimentID " queryString &amp;= "Select cmp.FacilityID, fac.FacilityName, cmp.CompDate, cmpfrm.FormComp, cmp.ReceivedFrom " queryString &amp;= "Order By FacilityName" queryString &amp;= "Where usrfac.Username=" &amp; strUsername If intFacilityID &lt;&gt; -1 Then queryString &amp;= " And fac.FacilityID = " &amp; intFacilityID End If</pre> <p><br> <br> &nbsp;</p> 2012-04-18T09:11:14-04:004949391http://forums.asp.net/p/1792198/4949391.aspx/1?Re+VB+NET+LINQ+to+SQL+Dynamic+Where+Clause+and+JoinsRe: VB.NET LINQ to SQL Dynamic Where Clause and Joins <p>Hi,</p> <p>Have you taken a look at the information discussed here? It explains the different types of queries supported in the Entity Framework:</p> <p>Querying a Conceptual Model <a href="http://msdn.microsoft.com/en-us/library/bb738642.aspx"> http://msdn.microsoft.com/en-us/library/bb738642.aspx</a></p> <p>Perhaps you could make use of an Alias, as discussed here:</p> <p>Query Builder Methods <a href="http://msdn.microsoft.com/en-us/library/bb896238.aspx"> http://msdn.microsoft.com/en-us/library/bb896238.aspx</a></p> <p>Thanks,</p> <p>Cathy Miller</p> 2012-04-24T20:13:57-04:00