2011-06-24 60 views
0

我正在使用Microsoft CRM 2011 Web服務檢索活動列表,但由於數據量太大,請求超時。我正在實現分頁,並試圖做出收益回報,但後來我得到了'連接意外關閉'的錯誤。使用Microsoft CRM限制結果

public IEnumerable<LinkedActivity> GetActivitiesForUser() { 
    var svc = GetCrmService(); 
    var cols = new ColumnSet(); 
    cols.Attributes = new[] { "activityid", "addressused", "scheduledstart", "scheduledend", "partyid", "activitypartyid", "participationtypemask", "ownerid" }; 
    var query = new QueryExpression(); 
    query.EntityName = EntityName.activityparty.ToString(); 
    query.ColumnSet = cols; 

    LinkEntity link = new LinkEntity(); 
    //link.LinkCriteria = filter; 
    link.LinkFromEntityName = EntityName.activitypointer.ToString(); 
    link.LinkFromAttributeName = "activityid"; 
    link.LinkToEntityName = EntityName.activityparty.ToString(); 
    link.LinkToAttributeName = "activityid"; 
    query.LinkEntities = new[] {link}; 

    var activities = svc.RetrieveMultiple(query); 
    var entities = new List<ICWebServices.activityparty>(); 
    RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse) svc.Execute(request); 
    //var pointers = new List<activitypointer>(); 
    foreach (activityparty c in activities.BusinessEntities) 
    { 
     //yield return c; - this returned an error "The connection was unexpectedly closed" 
     entities.Add(((activityparty)c)); 
    } 

實現分頁我用:

var activities = GetActivities(); 
    var offset = startRowIndex > 0 ? activities.Skip(startRowIndex) : activities; 
    var limited = limit > 0 ? offset.Take(limit) : offset.Take(100); 
    return limited.ToList(); 

有什麼辦法,我可以只返回前100個項目?

回答

0

tyrongower是正確的。此外,您應該將您的服務包裝在使用說明中,以確保其妥善處理。

0

爲什麼不在你的應用中使用LINQ和分頁?

Linq本身就有這個東西,MS有REST和SOAP端點。

我還沒有測試過,但我懷疑REST端點可以直接用於分頁。

public static class PagingExtensions 
{ 
//used by LINQ to SQL 
public static IQueryable<TSource> Page<TSource>(this IQueryable<TSource> source, int page, int pageSize) 
{ 
return source.Skip((page - 1)*pageSize).Take(pageSize); 
} 

//used by LINQ 
public static IEnumerable<TSource> Page<TSource>(this IEnumerable<TSource> source, int page, int pageSize) 
{ 
return source.Skip((page - 1)*pageSize).Take(pageSize); 
} 

} 

class Program 
{ 
static void Main(string[] args) 
{ 
List<string> names = new List<string>(); 
names.AddRange(new string[]{"John","Frank","Jeff","George","Bob","Grant", "McLovin"}); 
foreach (string name in names.Page(2, 2)) 
{ 
Console.WriteLine(name); 
} 

} 
}