[英]Lazy loading repeater has linq query not yielding results
所以我很懶地加載一個轉發器控件:
后面的代碼將轉發器綁定到guestbookData屬性,該屬性由loadGuestbook()填充
public partial class _Default
{
private DataTable _guestbookData;
public DataTable guestbookData
{
get
{
if (_guestbookData == null)
{
_guestbookData = loadGuestbook();
}
return _guestbookData;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBind();
}
}
private DataTable loadGuestbook()
{
netguestData nd = new netguestData();
List<post> data = nd.GetPosts(10, rptGuestbook.Items.Count);
DataTable dt = new DataTable();
// writing the list to a DataTable for bind happens here.
// not sure how to cast IEnumerable to DataTable, else I'd do that
return dt;
}
protected void btnLoadMore_Click(object sender, EventArgs e)
{
DataBind();
}
}
使用LINQ To SQL從數據庫中查詢數據。 這是我正在使用的GetPosts(int,int)函數:
public class netguestData
{
private netguestEntities ne = new netguestEntities();
public netguestData()
{
}
public List<post> GetPosts(int Take, int Skip)
{
var posts = (from p in ne.posts
.OrderByDescending(p => p.postdate)
.Take(Take)
.Skip(Skip)
select p).ToList();
return posts;
}
}
現在,要對此進行分頁,我基本上每頁加載10行,並使用轉發器中的項目計數作為所選數據中要跳過多少行的參考。
第一次加載頁面時,我獲得了最初的10條記錄,沒有任何問題,但是當我單擊按鈕加載下一組記錄時,它變成空白。
調試器中的消息是:
枚舉沒有結果
單擊后,我已經檢查了Take
匯整Take
和“ Skip
值,並且兩者均為10,符合預期。 表中有200多個行,所以我不明白問題是什么。
有人可以建議我做些什么來解決此問題嗎?
提前致謝!
您可能要先跳過然后再接聽。 (先走然后跳就沒有多大意義。)
通過查詢來說明原因:
var posts = (from p in ne.posts
.OrderByDescending(p => p.postdate)
.Take(Take)
.Skip(Skip)
select p).ToList();
第一次嘗試時,您需要10條帖子,而跳過這10條帖子中的0條。
在下一次嘗試中,您將獲得10條帖子,而跳過這10條帖子中的10條 ,顯然沒有結果。
編寫本文時,您總是在查詢10個最新結果,除了第一次跳過0並跳過所有結果。
就像@Becuzz所說的那樣,您只想交換跳過和獲取,以便跳過從原始查詢返回的結果並從其余查詢獲取的結果。
var posts = (from p in ne.posts
.OrderByDescending(p => p.postdate)
.Skip(Skip)
.Take(Take)
select p).ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.