C# 尝试从QBO API获取超过1000个客户和发票时出现异常

C# 尝试从QBO API获取超过1000个客户和发票时出现异常,c#,asp.net,quickbooks-online,C#,Asp.net,Quickbooks Online,我有1000多个客户和发票,我正在尝试将所有这些客户和发票提取到下拉列表中 QBO站点上的文档表明,如果我想在一个网格中加载所有客户,我们应该使用分页,但我想要的是在下拉列表中加载所有客户和发票 当我尝试获取1000多个客户和发票时,我遇到以下异常: 引发了验证异常 详细信息:QueryValidationError:值100000太大。最大允许值为1000 我正在尝试使用以下代码获取所有客户 public static List<Customer> GetAllQBOCustome

我有1000多个客户和发票,我正在尝试将所有这些客户和发票提取到下拉列表中

QBO站点上的文档表明,如果我想在一个网格中加载所有客户,我们应该使用分页,但我想要的是在下拉列表中加载所有客户和发票

当我尝试获取1000多个客户和发票时,我遇到以下异常:

引发了验证异常

详细信息:QueryValidationError:值100000太大。最大允许值为1000

我正在尝试使用以下代码获取所有客户

public static List<Customer> GetAllQBOCustomers(ServiceContext context)
{
   return Helper.FindAll<Customer>(context, new Customer(),1,100000);
}
公共静态列表GetAllQBOCustomers(ServiceContext上下文)
{
return Helper.FindAll(上下文,new Customer(),1100000);
}

您不能一次下载所有这些记录。这就是错误告诉你的——非常清楚。没有什么神奇的方法可以避免服务器的规则

然而,我真的认为你不应该一次下载它们。下拉列表不是向用户显示该数量数据的好方法。考虑用户体验-你想通过一个数以千计的客户列表来尝试找到你想要的吗?还是更容易开始输入名字的一部分,让它弹出一个可能匹配的短列表来选择


实现这一点的一种更为用户友好的方法是使用自动完成框而不是下拉列表,在用户键入几个字符后,它可以使用AJAX在API中搜索名称或ID包含这些字符的客户。这样,您每次只需返回少量记录,用户就不必为了在10000条记录列表的底部找到客户而滚动10分钟

简单的答案是循环足够的时间来获取所需的记录:

public static List<Customer> GetAllQBOCustomers(ServiceContext context)
{
    var list = new List<Customer>();
    for (int i=0; i<=10000; i+= 1000)
    {
        var results = Helper.FindAll<Customer>(context, new Customer(),i, 1000);
        list.AddRange(results);
    }
    return list;
}
公共静态列表GetAllQBOCustomers(ServiceContext上下文)
{
var list=新列表();
对于(int i=0;i
{
var results=Helper.FindAll(上下文,new Customer(),i*1000,1000);
bag.AddRange(结果);
});
返回包.ToList();
}

由于这一系列调用可能很昂贵,我建议您缓存结果。

我编写了以下代码并解决了我的问题

1. First I get the count of all the customers 
2. Then I get all the customers in chunks and the chunk size is 1000 
3. Create a List for customers. 
4. Define 3 integer type variables for counting. 
5. After that use do-while loop  
6. Add all the customers are added to the main customer list


        string strQuery = "Select Count(*) From Customer";
        string custCount = qboAccess.GetCutomerCount(qboInz.QboServiceContext, strQuery);
        List<qboData.Customer> customers = new List<Customer>();
        int maxSize = 0;
        int position = 1;
        int count = Convert.ToInt32(custCount);
        do
        {
          var custList = qboAccess.GetAllQBOEntityRecords(qboInz.QboServiceContext, new Customer(), position, 1000);
          customers.AddRange(custList);
          maxSize += custList.Count();
          position += 1000;
        } while (count > maxSize);
1。首先我要计算所有顾客的数量
2.然后我把所有的客户分成块,块大小是1000
3.为客户创建一个列表。
4.定义3个整数类型变量进行计数。
5.然后使用DoWhile循环
6.添加将所有客户添加到主客户列表中
string strQuery=“从客户选择计数(*)”;
字符串custCount=qboAccess.GetCutomerCount(qboInz.QboServiceContext,strQuery);
列出客户=新列表();
int maxSize=0;
int位置=1;
int count=Convert.ToInt32(custCount);
做
{
var custList=qboAccess.GetAllQBOEntityRecords(qboInz.QboServiceContext,new Customer(),position,1000);
customers.AddRange(custList);
maxSize+=custList.Count();
位置+=1000;
}而(计数>最大值);

所以,你的问题标题是“一百万”,你的代码是“十万”,例外情况是“你不能检索超过1000个”。我在这里看到了一个轻微的“注意细节”问题。我正在使用剑道网格加载所有客户和发票,并且我为剑道网格禁用了server operation=false,这就是为什么我不想向服务器发送任何请求。我只想一次加载所有客户和发票。为了清楚起见,我在上面提到,我想在下拉列表中加载所有记录谢谢你的建议。亲爱的,我知道你在上面提出的所有建议,但有些时候客户的要求非常奇怪,他们想要的正是他们所建议的,所以我们无法为他们提出更好的解决方案,这就是我发布该问题的原因,我解决了上述问题,并将我的答案张贴在这里。感谢您的快速回复和良好的建议。我了解客户的所有情况……但有时作为技术人员,我们的工作就是礼貌地告诉他们我们想到了一个更好的主意。根据我的经验,只要你解释说它使应用程序更易于使用,或者使它更便宜、更快,或者无论它有什么好处,他们通常都会很乐意同意这个更改。在这种情况下,如果你不能事先说服他们,那么我认为他们可能会意识到问题所在,当他们真正开始尝试使用荒谬的下拉列表时:-)。祝你的项目好运。这会奏效,但我想可能会很慢
1. First I get the count of all the customers 
2. Then I get all the customers in chunks and the chunk size is 1000 
3. Create a List for customers. 
4. Define 3 integer type variables for counting. 
5. After that use do-while loop  
6. Add all the customers are added to the main customer list


        string strQuery = "Select Count(*) From Customer";
        string custCount = qboAccess.GetCutomerCount(qboInz.QboServiceContext, strQuery);
        List<qboData.Customer> customers = new List<Customer>();
        int maxSize = 0;
        int position = 1;
        int count = Convert.ToInt32(custCount);
        do
        {
          var custList = qboAccess.GetAllQBOEntityRecords(qboInz.QboServiceContext, new Customer(), position, 1000);
          customers.AddRange(custList);
          maxSize += custList.Count();
          position += 1000;
        } while (count > maxSize);