我想做一些类似于这个示例代码从菲尔哈克到VB,和LINQ Orderby给我的问题 - 我不知道如何做到这一点。整个方法发布为completenes。LINQ - 动态OrderBy在VB.Net
这是C#版本:
public ActionResult DynamicGridData(string sidx, string sord, int page, int rows)
{
var context = new HaackOverflowDataContext();
int pageIndex = Convert.ToInt32(page) - 1;
int pageSize = rows;
int totalRecords = context.Questions.Count();
int totalPages = (int)Math.Ceiling((float)totalRecords/(float)pageSize);
var questions = context.Questions.OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize);
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from question in questions
select new
{
i = question.Id,
cell = new string[] { question.Id.ToString(), question.Votes.ToString(), question.Title }
}).ToArray()
};
return Json(jsonData);
}
我的问题是这一行...:
var questions = context.Questions.OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize);
在VB.Net排序依据不接受字符串作为价值 - 它似乎在C#中做到这一点(或者我错过了一些东西)。
(请不使用VAR不是这里的问题,我有一个覆盖:))。
编辑: 这是我得到(我根本无法编译)错误:
重载决策失败,因为没有可访问的“排序依据”可以用这些参数调用...
截图完整的错误消息:alt text http://i39.tinypic.com/u0wg0.png
编辑2:
根据要求提供更多信息。 SIDX包含排序的列的名称按 SORD包含ASC或DESC
的VB-代码:
Function MemberData(ByVal sidx As String, ByVal sord As String, ByVal page As Integer, ByVal rows As Integer) As JsonResult
Dim allRecords As IQueryable(Of Models.Member) = Me.MemberRepository.FindAllMembers
Dim currentPageRecords As IQueryable(Of Models.Member)
Dim pageIndex As Integer = page - 1
Dim pageSize As Integer = rows
Dim totalRecords As Integer = allRecords.Count
Dim totalPages As Integer = CInt(Math.Ceiling(totalRecords/pageSize))
Dim orderBy As String = sidx + " " + sord
currentPageRecords = allRecords.OrderBy(Function(m) orderBy).Skip(pageIndex * pageSize).Take(pageSize)
Dim jsonData = New With { _
.total = totalPages, _
.page = page, _
.records = totalRecords, _
.rows = New ArrayList _
}
For Each member As Models.Member In currentPageRecords
jsonData.rows.Add(New With {.id = member.MemberId, .cell = GenerateCellData(member)})
Next
Return Json(jsonData)
End Function
什么是您所看到的错误? – shahkalpesh 2009-05-27 19:28:49
只是在原始问题中增加了一些信息。 :) – Kjensen 2009-05-27 19:37:01
字符串sidx和sord包含什么? – CoderDennis 2009-05-27 19:53:11