Best way of constructing dynamic sql queries in C#/.NET3.5? -
a project i'm working on @ moment involves refactoring c# com object serves database access layer sql 2005 databases.
the author of existent code has built sql queries manually using string , many if-statements construct complex sql statement (~10 joins, >10 sub selects, ~15-25 conditions , groupby's). base table same one, structure of joins, conditions , groupings depend on set of parameters passed class/method.
constructing sql query work isn't elegant solution (and rather hard read/understand , maintain well)... write simple "querybuilder" myself pretty sure not first 1 kind of problem, hence questions:
- how you construct database queries?
- does c# offer easy way dynamically build queries?
i used c# , linq similar log entries filtered on user input (see conditional linq queries):
iqueryable<log> matches = m_locator.logs; // users filter if (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: query isn't performed until .tolist() in last statement.
Comments
Post a Comment