This question is locked. New answers and comments are not allowed.
Hi,
I have a service layer between my web site and my repository so I cannot expose an IQueryable to the grid. Therefore I am trying to create a service function that takes the sortOrder collection and the IFilterDescriptor collection and translates them into a linq query against the repository, returning the correct listing of business objects to the controller.
The sorting was not a problem, but I am confused as to how to go about the filtering.
So far I have:
Can you point me at an example of how I would take the IFilterDescriptors and use them to filter my repository?
Many thanks,
Nick
I have a service layer between my web site and my repository so I cannot expose an IQueryable to the grid. Therefore I am trying to create a service function that takes the sortOrder collection and the IFilterDescriptor collection and translates them into a linq query against the repository, returning the correct listing of business objects to the controller.
The sorting was not a problem, but I am confused as to how to go about the filtering.
So far I have:
| Public Function GetAudits(ByVal index As Integer, _ |
| ByVal pageSize As Integer, _ |
| ByVal sortOrders As List(Of Telerik.Web.Mvc.SortDescriptor), _ |
| ByVal filters As List(Of Telerik.Web.Mvc.IFilterDescriptor)) As PagedList(Of PortalAudit) Implements IPortalAuditService.GetAudits |
| Dim audits = From a In _repository.GetAudits Select a |
| ' filtering |
| If filters.Count > 0 Then |
| ' DO SOMETHING HERE.... |
| End If |
| ' sort order |
| If sortOrders.Count = 0 Then |
| audits = audits.OrderByDescending(Function(a) a.DateStamp) |
| Else |
| For Each order In sortOrders |
| AddSortOrder(audits, order) |
| Next |
| End If |
| ' Return paged list of audit records. |
| Return (From a In audits Select New PortalAudit With {.Action = a.Action, _ |
| .AffectedEntity = a.AffectedEntity, _ |
| .AuditId = a.AuditId, _ |
| .Controller = a.Controller, _ |
| .DateStamp = a.DateStamp, _ |
| .Description = a.Description, _ |
| .Status = a.Status, _ |
| .UserName = a.UserName}).ToPagedList(index, pageSize) |
| End Function |
| Private Shared Sub AddSortOrder(ByRef audits As System.Linq.IQueryable(Of Audit), ByVal order As SortDescriptor) |
| Select Case order.Member.ToLower |
| Case "action" |
| If order.SortDirection = ComponentModel.ListSortDirection.Ascending Then |
| audits = audits.OrderBy(Function(a) a.Action) |
| Else |
| audits = audits.OrderByDescending(Function(a) a.Action) |
| End If |
| Case "affectedentity" |
| If order.SortDirection = ComponentModel.ListSortDirection.Ascending Then |
| audits = audits.OrderBy(Function(a) a.AffectedEntity) |
| Else |
| audits = audits.OrderByDescending(Function(a) a.AffectedEntity) |
| End If |
| Case "auditid" |
| If order.SortDirection = ComponentModel.ListSortDirection.Ascending Then |
| audits = audits.OrderBy(Function(a) a.AuditId) |
| Else |
| audits = audits.OrderByDescending(Function(a) a.AuditId) |
| End If |
| Case "controller" |
| If order.SortDirection = ComponentModel.ListSortDirection.Ascending Then |
| audits = audits.OrderBy(Function(a) a.Controller) |
| Else |
| audits = audits.OrderByDescending(Function(a) a.Controller) |
| End If |
| Case "datestamp" |
| If order.SortDirection = ComponentModel.ListSortDirection.Ascending Then |
| audits = audits.OrderBy(Function(a) a.DateStamp) |
| Else |
| audits = audits.OrderByDescending(Function(a) a.DateStamp) |
| End If |
| Case "description" |
| If order.SortDirection = ComponentModel.ListSortDirection.Ascending Then |
| audits = audits.OrderBy(Function(a) a.Description) |
| Else |
| audits = audits.OrderByDescending(Function(a) a.Description) |
| End If |
| Case "status" |
| If order.SortDirection = ComponentModel.ListSortDirection.Ascending Then |
| audits = audits.OrderBy(Function(a) a.Status) |
| Else |
| audits = audits.OrderByDescending(Function(a) a.Status) |
| End If |
| Case "username" |
| If order.SortDirection = ComponentModel.ListSortDirection.Ascending Then |
| audits = audits.OrderBy(Function(a) a.UserName) |
| Else |
| audits = audits.OrderByDescending(Function(a) a.UserName) |
| End If |
| End Select |
| End Sub |
Can you point me at an example of how I would take the IFilterDescriptors and use them to filter my repository?
Many thanks,
Nick