【发布时间】:2009-05-27 19:25:35
【问题描述】:
我正在尝试执行与从 Phil Haack 到 VB 的示例代码类似的操作,而 LINQ Orderby 给我带来了问题 - 我不知道该怎么做。为完整性发布了整个方法。
这是 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 中 OrderBy 不接受字符串作为值 - 它似乎在 C# 中这样做(或者我遗漏了一些东西)。
(请不要使用 VAR 不是这里的问题,我已经涵盖了。:))
编辑: 这是我得到的错误(我根本无法编译):
重载解析失败,因为无法使用这些参数调用可访问的“OrderBy”...
编辑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
【问题讨论】:
-
您看到的错误是什么?
-
刚刚在原始问题中添加了更多信息。 :)
-
字符串 sidx 和 sord 包含什么?
-
您能否也发布包含错误的 VB 代码?
-
Dennis Palmer>> 添加了 vb 代码和更多信息。 :)