在多次迭代中从 QB 文件中检索数据

Retrieving data from QB file in multiple iterations

我正在使用以下代码从 QB 文件中获取公司名称和其他数据 QB-SDK:

public IList<CustomerModelQB> GetAllCustomer(string fromName = "a", string toName = "z", bool IsActiveOnly = true)
{
    RequestMsgSet.ClearRequests();
    ICustomerQuery CustomerQueryRq = RequestMsgSet.AppendCustomerQueryRq();

    if (IsActiveOnly)
    {
        if (CustomerQueryRq != null)
                CustomerQueryRq.ORCustomerListQuery.CustomerListFilter.ActiveStatus.SetValue(
                ENActiveStatus.asActiveOnly);
    }
    else
        CustomerQueryRq.ORCustomerListQuery.CustomerListFilter.ActiveStatus.SetValue(ENActiveStatus.asAll);


    //Set field value for FromName
    CustomerQueryRq.ORCustomerListQuery.CustomerListFilter.ORNameFilter.NameRangeFilter.FromName.SetValue(fromName);
    //Set field value for ToName
    CustomerQueryRq.ORCustomerListQuery.CustomerListFilter.ORNameFilter.NameRangeFilter.ToName.SetValue(toName);

    CustomerQueryRq.IncludeRetElementList.Add("FullName");
    CustomerQueryRq.IncludeRetElementList.Add("AccountNumber");

    ResponseMsgSet = SessionManager.DoRequests(RequestMsgSet);

    return WalkCustomerQuery(ResponseMsgSet);
}

我查看了 iterator 并尝试了一些代码..

它似乎正在获取初始数据,例如获取前一千条记录,仅此而已...逻辑就像获取数据的前几条记录,然后是接下来的几条记录,依此类推,直到总计获取的记录....但不幸的是,QB SDK 没有提供此功能,因为它只让我获取前几条记录,仅此而已...

其实我想做的是:

我的 QB 公司文件中只有很少的 100k 条记录,我想获取前几条记录(如一万条记录),然后移至接下来的 10000 条记录,然后再移至接下来的 10000 条记录,依此类推...直到获取所有记录。

尽管我可以使用 ORNameFilterTotalBalanceFilter 和其他一些过滤器来做到这一点,但我想像前 10,000 条记录一样这样做比下一个 10,000,依此类推,直到我公司文件中的记录总数。

这实际上是 SO 问题的延续。

有没有办法做到这一点?

这里是我们的代码,用于从日期范围内获取发票。它使用 QBFC。

    public List<tbInvoiceHeader> GetInvoices(DateTime? fromDate, DateTime? toDate, bool fromModifiedDate, string invoiceNumber)
    {
        var invoices = new List<tbInvoiceHeader>();

        IMsgSetRequest requestMsgSet;
        IMsgSetResponse responseMsgSet;

        requestMsgSet = GetLatestMsgSetRequest();
        requestMsgSet.Attributes.OnError = ENRqOnError.roeContinue;

        IInvoiceQuery invoiceQuery = requestMsgSet.AppendInvoiceQueryRq();
        IInvoiceFilter invoiceFilter = invoiceQuery.ORInvoiceQuery.InvoiceFilter;

        if (!string.IsNullOrEmpty(invoiceNumber))
        {
            invoiceFilter.ORRefNumberFilter.RefNumberFilter.RefNumber.SetValue(invoiceNumber);
            invoiceFilter.ORRefNumberFilter.RefNumberFilter.MatchCriterion.SetValue(ENMatchCriterion.mcStartsWith);
        }
        else
        {
            if (fromDate.HasValue)
            {
                if (!fromModifiedDate)
                {
                    invoiceFilter.ORDateRangeFilter.TxnDateRangeFilter.ORTxnDateRangeFilter.TxnDateFilter.FromTxnDate.SetValue(fromDate.Value);
                }
                else
                {
                    invoiceFilter.ORDateRangeFilter.ModifiedDateRangeFilter.FromModifiedDate.SetValue(fromDate.Value, asDateOnly: true);
                }
            }

            if (toDate.HasValue)
            {
                if (!fromModifiedDate)
                {
                    invoiceFilter.ORDateRangeFilter.TxnDateRangeFilter.ORTxnDateRangeFilter.TxnDateFilter.ToTxnDate.SetValue(toDate.Value);
                }
                else
                {
                    invoiceFilter.ORDateRangeFilter.ModifiedDateRangeFilter.ToModifiedDate.SetValue(toDate.Value, asDateOnly: true);
                }
            }
        }

        invoiceFilter.MaxReturned.SetValue(iterationNumber); // Set max returns element.
        invoiceQuery.iterator.SetValue(ENiterator.itStart);
        invoiceQuery.IncludeLinkedTxns.SetValue(true);
        invoiceQuery.IncludeLineItems.SetValue(true);   
        invoiceQuery.OwnerIDList.Add("0");              // To include customs fields

        responseMsgSet = mySessionManager.DoRequests(requestMsgSet);
        do 
        {
            //Step 5: Interpret the response
            IResponseList rsList = responseMsgSet.ResponseList;

            //Retrieve the one response corresponding to our single request
            IResponse response = rsList.GetAt(0);

            if (response.StatusCode == 0) //We have one or more invoices-> show them
            {
                IInvoiceRetList invoiceList = response.Detail as IInvoiceRetList;
                int maxCnt = invoiceList.Count;

                if (invoiceProgressEvent != null)
                {
                    invoiceProgressEvent(new ProgressEvent() { Count = maxCnt, RemainingCnt = response.iteratorRemainingCount, Invoices = invoices });
                }

                //for logging only
                //XmlDocument doc = new XmlDocument();
                //doc.LoadXml(responseMsgSet.ToXMLString());
                //XmlNodeList nodes = doc.SelectNodes("//InvoiceRet");
                for (int ndx = 0; ndx < maxCnt; ndx++)
                {
                    //var xmlText = nodes[ndx].InnerXml;
                    IInvoiceRet invoiceRet = invoiceList.GetAt(ndx);
                    invoices.Add(GetInvoiceHeaderDetail(invoiceRet));
                }
            }

            if (response.iteratorRemainingCount > 0)
            {
                invoiceQuery.iteratorID.SetValue(response.iteratorID);
                invoiceQuery.iterator.SetValue(ENiterator.itContinue);
                responseMsgSet = mySessionManager.DoRequests(requestMsgSet);
            }
            else
            {
                //This cause The iteratorID "..." is not valid.
                //invoiceQuery.iteratorID.SetValue(response.iteratorID);
                //invoiceQuery.iterator.SetValue(ENiterator.itStop);
                //responseMsgSet = mySessionManager.DoRequests(requestMsgSet);
                break;
            }
        } while (true);

        return invoices;
    }