我正在使用Microsoft CRM 2011网络服务来检索活动列表,但由于数据量的原因,请求已超时。我正在实现分页并尝试进行yield return,但后来我得到了“连接意外关闭”错误。
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 :(得分:2)
您需要在QueryExpression中定义PageInfo
答案 1 :(得分:0)
答案 2 :(得分: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);
}
}
}