|
Here is an example of how I did it with
multiple sort fields: (SortCriterion has 2 properties: Column
and Order) DAO code: ///
<summary> ///
Gets a list of CrossReference objects for the given table name. ///
The list will be sorted according to the specified sort criteria. ///
</summary> ///
<param
name="tableName">The name of the CrossReference table</param> ///
<param
name="sortCriteria"> ///
A list of SortCriterion specifying how the list should be sorted ///
</param> ///
<returns> ///
A list of CrossReference objects for the specified table ///
</returns> public IList<CrossReference>
GetCrossReferencesByTableName( string tableName, IList<SortCriterion> sortCriteria) { // Remove criteria duplicates if (sortCriteria != null) { IDictionary<string, SortCriterion> criteriaMap = new Dictionary<string, SortCriterion>(sortCriteria.Count); foreach (SortCriterion criterion in sortCriteria) { if
(!criteriaMap.ContainsKey(criterion.Column)) {
criteriaMap.Add(criterion.Column, criterion); } } sortCriteria
= new List<SortCriterion>(criteriaMap.Values); } IDictionary args = new Hashtable(); args.Add("tableName",
tableName); // the keys will be used as the names in the mapping args.Add("SortCriteria",
sortCriteria); IList<CrossReference> crossReferenceList =
SqlMapper.QueryForList<CrossReference>( "CrossReference.SelectWithSort",
args); return crossReferenceList; } Mapping: [some names removed] <select id="Select" resultMap="CrossReference.Result" parameterClass="string" cacheModel="CrossReferenceCache"> SELECT A, B, C FROM Xra.Xra$tableName$Xref </select> <select id="SelectWithSort" resultMap="CrossReference.Result" parameterClass="map" <!-- parameter class must be a
map so that the table name can be used in the map above --> extends="Select" cacheModel="CrossReferenceCache"> <isNotNull property="SortCriteria"> <iterate property="SortCriteria" conjunction="," prepend=" ORDER BY ">
$SortCriteria[].Column$ $SortCriteria[].Order$ </iterate> </isNotNull> </select> Does this make sense? You only need a map if you have other
parameters to pass in besides the sort criteria, such as a value in the where
clause. From: Stief Dirckx
[mailto:[EMAIL PROTECTED]
|
- dynamic order by clause Stief Dirckx
- RE: dynamic order by clause Gentile, Wayne \(Hitachi Consulting\)
- RE: dynamic order by clause Stief Dirckx
- RE: dynamic order by cl... Gentile, Wayne \(Hitachi Consulting\)

