Hi Chirag,
Sorry for the frustration, but I think there is a little bit of communitacion problems
Ok, first of all, I don't see any obvious error in Walaa's code. Anyway, I don't know why you started using DerivedTable and DynamicRelations as from your first posts, the problem seems quite simple: You want your clients, ordered by a CASE...END expression. So let's start from that.
There are two ways (among others, but let's keep this simple) you can achieve this:
A. Use DynamicList (note that this is not DynamicRelation which is rarely used, just use this for advance joins).
Maybe a Northwind example would help here. Say I want to fetch Employees that ultimately will generate a query like this:
SELECT
EmployeeID,
FirstName,
LastName,
CASE
WHEN FirstName LIKE '%d%' THEN 1
WHEN LastName LIKE '%d%' THEN 2
ELSE 3
END AS Priority
FROM Employees
ORDER BY Priority
The code that achieve above would look like:
// define my desired fields
var fields = new ResultsetFields(4);
fields.DefineField(EmployeeFields.EmployeeId, 0);
fields.DefineField(EmployeeFields.FirstName, 1);
fields.DefineField(EmployeeFields.LastName, 2);
// this is the epxression field
var pattern = "%d%";
var expr = new DbFunctionCall("CASE WHEN {0} LIKE {1} THEN {2} WHEN {3} LIKE {4} THEN {5} ELSE {6} END",
new object[] { EmployeeFields.FirstName, pattern, 1, EmployeeFields.LastName, pattern, 2, 3 });
var specialField = new EntityField("Priority", expr, typeof(int));
fields.DefineField(specialField, 3);
// I want to sort on that special field
var sortExpr = new SortExpression(specialField | SortOperator.Ascending);
// fetch
DataTable dynamicList = new DataTable();
TypedListDAO dao = new TypedListDAO();
dao.GetMultiAsDataTable(fields, dynamicList, sortExpr, null, null, true, null);
// tests
Assert.AreEqual(2, dynamicList.Rows[0].ItemArray[0]);
Assert.AreEqual(8, dynamicList.Rows[dynamicList.Rows.Count - 1].ItemArray[0]);
B. Fetch EmployeeCollection and sort on that CASE expression. This wont fetch the expression as part of the fields (that Priority filed), but you can sort on that. This is the code that would generate above sql:
var employees = new EmployeeCollection();
// sort on an special field
var pattern = "%d%";
var expr = new DbFunctionCall("CASE WHEN {0} LIKE {1} THEN {2} WHEN {3} LIKE {4} THEN {5} ELSE {6} END",
new object[] { EmployeeFields.FirstName, pattern, 1, EmployeeFields.LastName, pattern, 2, 3 });
var specialField = new EntityField("Priority", expr, typeof(int));
var specialSortClause = new SortClause(specialField, SortOperator.Ascending);
specialSortClause.EmitAliasForExpressionAggregateField = false;
var sortExpr = new SortExpression(specialSortClause);
// fetch
employees.GetMulti(null, 0, sortExpr);
// tests
Assert.AreEqual(2, employees.First().EmployeeId);
Assert.AreEqual(8, employees.Last().EmployeeId);
... the trick is made by the specialSortClause.EmitAliasForExpressionAggregateField = false; line.
I think that those two approaches are enough for you. If you like the 2nd approach but you want to have a "Priority" property on your entity you can implement a custom property as I explained in my first post.
So, now you just have to transport this concept to your code. Let us know if you need further help on this.