2014-06-10 70 views
0

我們有一個Silverlight應用程序調用Web服務從SQL Server檢索數據庫行。這些然後顯示在分頁控件的屏幕上。但是,整個表是必需的,它由幾千行組成。在客戶系統上,如果我們要求超過大約1500行,我們會得到HttpRequestTimedOutWithoutDetail。在我們的開發系統中,我們需要大約500,000行纔會發生這種情況。Silverlight分頁web服務結果

顯然,我們應該做的是分頁結果並將它們返回到Silverlight中。但我不知道該怎麼做。任何人都可以提出建議,或點我到一些網站的頁面,明確解釋的原則和方法(我有點簡單!)

這裏是在Web服務代碼:

public IQueryable<Referral> GetReferrals() 
    { 
     /* 
     * In the customer's environments it seems that any more than around 1500 referrals will break the system: they will fail to load. 
     * In the dev environment is takes around 500,000 so it seems to be timeout related. 
     * 
     * The code below was an attempt to restrict the number, only returning referrals above a certain size and within a certain age. 
     * It seems the customer is more interested in the smaller referrals though since they are more likely to be added to existing 
     * substations so if this method is re-instated, we should be using '<' instead of '>' 
     * 
     int mdToBeMet = int.Parse(ConfigurationManager.AppSettings["ReferralMDToBeMet"]); 
     DateTime minusNYears = DateTime.Today.AddYears(int.Parse(ConfigurationManager.AppSettings["ReferralTargetDate"]) * -1); 
     int maxReferralsCount = int.Parse(ConfigurationManager.AppSettings["ReferralMaxRecordCount"]); 
     if (mdToBeMet != 0 && maxReferralsCount != 0) 
     { 
      return this.ObjectContext.Referrals.Where(x => x.MD_to_be_Met > mdToBeMet && x.Target_Date > minusNYears).OrderByDescending(y => y.Target_Date).Take(maxReferralsCount); 
     } 
     */ 

     /* 
     * This is the 2nd attempt: the customer is mainly interested in referrals that have an allocated substation 
     */ 
     bool allocatedReferralsOnly = bool.Parse(ConfigurationManager.AppSettings["ReferralAllocatedOnly"]); 
     int maxReferralsCount = int.Parse(ConfigurationManager.AppSettings["ReferralMaxRecordCount"]); 

     if (allocatedReferralsOnly) 
     { 
      var referrals = this.ObjectContext.Referrals.Where(x => x.Sub_no != "").OrderByDescending(y => y.Target_Date).Take(maxReferralsCount); 
      return referrals; 
     } 
     else 
     { 

      /* 
      * Ideally, we should just page the referrals here and return to retrieving all of them, bit by bit. 
      */ 
      var referrals = this.ObjectContext.Referrals; 
      return referrals; 
     } 
    } 

非常感謝有什麼建議麼。

+0

假設你使用LINQ來查詢你的db,看看'skip'和'take'。然後,你可以做一些像'var result = context.Customers.Skip(pageSize * pageNumber).Take(pageSize)' – Mashton

+0

我已經將代碼添加到問題中。我們的解決方法是隻獲得最重要的行,並進一步限制爲1500(使用'Take'),如果不止於此。你是否建議我使用Skip循環遍歷,取1500行並將它們添加到結果中,直到獲得全部內容(返回的行數 user41013

+0

如果這是LINQ2SQL或類似的東西,那麼使用'Skip'將會被翻譯成sql ...所以它不會帶回X記錄來跳過它們:生成的sql會跳過。 – Mashton

回答

0

對我給的註釋擴大的一個例子...

/// <summary> 
/// Returns a page of Referrels 
/// pageNumber is 0-index based 
/// </summary> 
public IQueryable<Referrel> GetReferrals(int pageNumber) 
{ 
    var pageSize = 100; 
    return ObjectContext.Referrals.Skip(pageNumber*pageSize).Take(pageSize); 
} 

很明顯,你可以通過在pageSize太多,如果你想要的,或者某個地方把它定義爲一個常量這種方法之外。