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 -
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 = ' + @strUsername
if @intFacility <> '-1'
set @sql = @sql + ' and Facility = ' + @intFacility
if @param3 <> ''
set @sql = @sql + ' and field3 = ' + @param3
if @param4 <> ''
set @sql = @sql + ' and field4 = ' + @param4
exec(@sql)
blued
Member
125 Points
163 Posts
Re: VB.NET LINQ to SQL Dynamic Where Clause and Joins
Apr 16, 2012 07:50 AM|LINK
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 -
Sorry if I've misunderstood what you mean?