我只需抓取一行,并将其名称和详细信息属性输出到我页面上的边框。
到目前为止,我已经提出了这个问题,它无法正常工作并在实体和列表之间发出错误。
Public Shared Function GetOneRow() As String
Dim db As New Model.Entities
Dim rowCount As Integer = (From t In db.Table Select t).Count
Dim randomNumber As Integer = New Random().Next(rowCount)
Dim query = (From t In db.Table Select t Where t.id = randomNumber).FirstOrDefault
Dim rowList As New List(Of Model.Table)
rowList.Add(query)
Dim htmlString As New StringBuilder
htmlString.Append("<h1>" & rowList(0).name.ToString & "</h1>")
htmlString.Append("<p>" & rowList(0).details.ToString & "</p>")
Return htmlString.ToString
End Function
还尝试过:
Public Shared Function GetOneRow() As String
Dim db As New Model.Entities
Dim query = From t In db.Table Select t
Dim count As Integer = query.Count
Dim index As Integer = New Random().Next(count)
Dim list As New List(Of Model.Table)
list.AddRange((query.Skip(index).FirstOrDefault))
Dim htmlstring As New StringBuilder
htmlstring.Append("<h1>" & list(0).name.ToString & "</h1>")
htmlstring.Append("<p>" & list(0).details.ToString & "</p>")
Return htmlstring.ToString
End Function
这里的错误是:只有LINQ to Entities中的排序输入才支持'Skip'方法。必须在方法'Skip'
之前调用'OrderBy'方法是否有更简单的方法从表中选择一个随机行,并将其名称和详细信息属性添加到我的stringbuilder中?
非常感谢您提供任何帮助。
答案 0 :(得分:3)
以下代码适用于我在C#中,对不起我在VB中不是很好
var qry = From t In db.Table Select t;
int count = qry.Count();
int index = new Random().Next(count);
return qry.Skip(index).FirstOrDefault();