@model System.Collections.IEnumerable
@(Html.Kendo().Grid<
HoghoughModel.PTStbKarkonan
>()
.Name("Grid")
.EnableCustomBinding(true)
.BindTo(Model)
.Columns(columns => {
columns.Bound(o => o.IssuePlace);
columns.Bound(o => o.BimehName);
columns.Bound(o => o.Family);
columns.Bound(o => o.Name);
})
.Pageable()
.Sortable()
.Filterable()
.Scrollable()
.Groupable()
.DataSource(dataSource => dataSource.Server().Total((int)ViewData["total"]))
)
public
ActionResult Index([DataSourceRequest(Prefix =
"Grid"
)] DataSourceRequest request)
{
if
(request.PageSize == 0)
{
request.PageSize = 10;
}
IQueryable<PTStbKarkonan> karkonan = db.PTStbKarkonans;
karkonan = karkonan.ApplyFiltering(request.Filters);
var total = karkonan.Count();
karkonan = karkonan.ApplySorting(request.Groups, request.Sorts);
karkonan = karkonan.ApplyPaging(request.Page, request.PageSize);
IEnumerable data = karkonan.ApplyGrouping(request.Groups);
ViewData[
"total"
] = total;
return
View(data);
}
public
static
class
PTStbKarkonanExtensions
{
public
static
IQueryable<PTStbKarkonan> ApplyPaging(
this
IQueryable<PTStbKarkonan> data,
int
page,
int
pageSize)
{
if
(pageSize > 0 && page > 0)
{
data = data.Skip((page - 1) * pageSize);
}
data = data.Take(pageSize);
return
data;
}
public
static
IEnumerable ApplyGrouping(
this
IQueryable<PTStbKarkonan> data, IList<GroupDescriptor>
groupDescriptors)
{
if
(groupDescriptors !=
null
&& groupDescriptors.Any())
{
Func<IEnumerable<PTStbKarkonan>, IEnumerable<AggregateFunctionsGroup>> selector =
null
;
foreach
(var group
in
groupDescriptors.Reverse())
{
if
(selector ==
null
)
{
if
(group.Member ==
"IssuePlace"
)
{
selector = Orders => BuildInnerGroup(Orders, o => o.IssuePlace);
}
else
if
(group.Member ==
"BimehName"
)
{
selector = Orders => BuildInnerGroup(Orders, o => o.BimehName);
}
else
if
(group.Member ==
"Family"
)
{
selector = Orders => BuildInnerGroup(Orders, o => o.Family);
}
else
if
(group.Member ==
"Name"
)
{
selector = Orders => BuildInnerGroup(Orders, o => o.Name);
}
}
else
{
if
(group.Member ==
"IssuePlace"
)
{
selector = BuildGroup(o => o.IDSeri, selector);
}
else
if
(group.Member ==
"BimehName"
)
{
selector = BuildGroup(o => o.BimehName, selector);
}
else
if
(group.Member ==
"Family"
)
{
selector = BuildGroup(o => o.Family, selector);
}
else
if
(group.Member ==
"Name"
)
{
selector = BuildGroup(o => o.Name, selector);
}
}
}
return
selector.Invoke(data).ToList();
}
return
data;
}
private
static
Func<IEnumerable<PTStbKarkonan>, IEnumerable<AggregateFunctionsGroup>>
BuildGroup<T>(Expression<Func<PTStbKarkonan, T>> groupSelector, Func<IEnumerable<PTStbKarkonan>,
IEnumerable<AggregateFunctionsGroup>> selectorBuilder)
{
var tempSelector = selectorBuilder;
return
g => g.GroupBy(groupSelector.Compile())
.Select(c =>
new
AggregateFunctionsGroup
{
Key = c.Key,
HasSubgroups =
true
,
Member = groupSelector.MemberWithoutInstance(),
Items = tempSelector.Invoke(c).ToList()
});
}
private
static
IEnumerable<AggregateFunctionsGroup> BuildInnerGroup<T>(IEnumerable<PTStbKarkonan>
group, Expression<Func<PTStbKarkonan, T>> groupSelector)
{
return
group.GroupBy(groupSelector.Compile())
.Select(i =>
new
AggregateFunctionsGroup
{
Key = i.Key,
Member = groupSelector.MemberWithoutInstance(),
Items = i.ToList()
});
}
public
static
IQueryable<PTStbKarkonan> ApplySorting(
this
IQueryable<PTStbKarkonan> data,
IList<GroupDescriptor> groupDescriptors, IList<SortDescriptor> sortDescriptors)
{
if
(groupDescriptors !=
null
&& groupDescriptors.Any())
{
foreach
(var groupDescriptor
in
groupDescriptors.Reverse())
{
data = AddSortExpression(data, groupDescriptor.SortDirection, groupDescriptor.Member);
}
}
if
(sortDescriptors !=
null
&& sortDescriptors.Any())
{
foreach
(SortDescriptor sortDescriptor
in
sortDescriptors)
{
data = AddSortExpression(data, sortDescriptor.SortDirection, sortDescriptor.Member);
}
}
return
data;
}
private
static
IQueryable<PTStbKarkonan> AddSortExpression(IQueryable<PTStbKarkonan> data, ListSortDirection
sortDirection,
string
memberName)
{
if
(sortDirection == ListSortDirection.Ascending)
{
switch
(memberName)
{
case
"IssuePlace"
:
data = data.OrderBy(order => order.IssuePlace);
break
;
case
"BimehName"
:
data = data.OrderBy(order => order.BimehName);
break
;
case
"Family"
:
data = data.OrderBy(order => order.Family);
break
;
case
"Name"
:
data = data.OrderBy(order => order.Name);
break
;
}
}
else
{
switch
(memberName)
{
case
"IssuePlace"
:
data = data.OrderByDescending(order => order.IssuePlace);
break
;
case
"BimehName"
:
data = data.OrderByDescending(order => order.BimehName);
break
;
case
"Family"
:
data = data.OrderByDescending(order => order.Family);
break
;
case
"Name"
:
data = data.OrderByDescending(order => order.Name);
break
;
}
}
return
data;
}
public
static
IQueryable<PTStbKarkonan> ApplyFiltering(
this
IQueryable<PTStbKarkonan> data,
IList<IFilterDescriptor> filterDescriptors)
{
if
(filterDescriptors !=
null
&& filterDescriptors.Any())
{
data = data.Where(ExpressionBuilder.Expression<PTStbKarkonan>(filterDescriptors));
}
return
data;
}
}
System.NotSupportedException: The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'