For my general grid, I am currently doing this to activate sorting:
Elements.OrderBy(column.SortExpression).AsQueryable();
In which SortExpression is a type Func<T, object>and the column is a generic classColumn<T>
I installed SortExpression in the controller as follows:
new Column<OrderViewData>{Key = "ShippingDate", SortExpression = e => e.ShippingDate}
"OrderBy" invokes the sql statement that I do not want.
So, I'm trying to replace it with this:
Elements = from element in Elements
orderby column.SortExpression
select element;
Which does not cause sql execution.
Now, the forcourse.SortExpression column should be of a different type. Only I can not understand what type it should be, and how to set it to a common class in the controller.
I can still install SortExpression in a generic style that is robust.
Any suggestions on how I can sort by an expression set somewhere else in the application, without executing sql when applying the order to IQueryable?
@Earwicker:
:
Expression<Func<Employee, DateTime?>> sortexpression = e => e.BirthDate;
var db = new NorthwindDataContext();
var query = from e in db.Employees
select e;
query = query.OrderBy(sortexpression);
int count = query.Count();
:
SELECT COUNT(*) AS [value]
FROM [dbo].[Employees] AS [t0]
DateTime? :
Expression<Func<Employee, object>> sortexpression = e => e.BirthDate;
InvalidOperationException: 'System.Object'
: " DateTime?", , . , Expression<Func<Employee, some_type>>. , - , SortExpression = e => e.BirthDate, "T".
, - , - e.BirthDate, Func<T, object> Expression<Func<T,some_type>>?
- , :
Elements.OrderBy(column.SortExpression.FixCast())
, . SQL- , .
, !
@earwicker 2:
var gridBuilder = new RainbowGridBuilder<Employee>("Examples_Employees")
{
Elements = GetEmployees(),
Columns = new List<Column<Employee>>{
new Column<Employee> {
Key = "EmployeeId",
Header = "Employee Id",
ValueExpression = e => e.EmployeeID.ToString(),
SortExpression = e => e.EmployeeID,
Display = false},
new Column<Employee> {Key = "Name", ValueExpression = e => e.FirstName + " " + e.LastName, SortExpression = e => e.LastName},
},
}