Best way of constructing dynamic sql queries in C#/.NET3.5?
I used C# and Linq to do something similar to get log entries filtered on user input (see Conditional Linq Queries):
IQueryable<Log> matches = m_Locator.Logs;// Users filterif (usersFilter) matches = matches.Where(l => l.UserName == comboBoxUsers.Text); // Severity filter if (severityFilter) matches = matches.Where(l => l.Severity == comboBoxSeverity.Text); Logs = (from log in matches orderby log.EventTime descending select log).ToList();
Edit: The query isn't performed until .ToList() in the last statement.
Unless executiontime is really important, I would consider refactoring the business logic that (so often) tends to find its way down to the datalayer and into gazillion-long stored procs. In terms of maintainabillity, editabillity and appendabillity I always try to (as the C# programmer I am) lift code up to the businesslayer.
Trying to sort out someone elses 8000 line SQL Script is not my favorite task.
:)
//W