上下文:C#、Xero.Api
我在 Invoices 上度过了一段有趣的时光。我编写了一些 C# 代码来接受来自命令行的查询字符串和文件名。我对 Xero 的端点运行查询并将结果存储在文件中。到目前为止,一切都很好。
但是,我发现在某些情况下会出现异常结果。
我正在使用以下 C# 代码:
private static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Syntax:\n\tXero \"<querystring>\" <outputspec>");
Environment.Exit(1);
}
var query = args[0];
var fspec = args[1];
Console.WriteLine($"{query}\n{fspec}");
// Private Application Sample
var xeroCoreApi = new XeroCoreApi("https://api.xero.com",
new PrivateAuthenticator(<not shown>),
new Consumer(<not shown>, <not shown>),
null,
new DefaultMapper(),
new DefaultMapper());
var org = xeroCoreApi.Organisation;
var user = new ApiUser { Name = Environment.MachineName };
var invoices = xeroCoreApi
.Invoices
.Where(query)
.Find();
var total = invoices.Count();
var page = 2;
StringBuilder sb = new StringBuilder();
sb.Append("<Invoices>");
if (invoices.Any())
{
foreach (Invoice I in invoices)
{
GatherInvoiceDetails(sb, I);
}
total = invoices.Count();
while (invoices.Count() == 100)
{
invoices = xeroCoreApi.Invoices.Page(page++).Find();
if (invoices.Any())
{
total += invoices.Count();
foreach (Invoice I in invoices)
{
GatherInvoiceDetails(sb, I);
}
}
}
}
sb.AppendLine("</Invoices>");
Console.WriteLine($"pages: {page-1}. invoices: {total}");
System.IO.File.WriteAllText(fspec, sb.ToString());
}
我有一个包含以下内容的批处理文件:
@echo off
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == ""AUTHORISED""" \temp\authorised.xml
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == ""DELETED""" \temp\deleted.xml
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == ""DRAFT""" \temp\draft.xml
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == ""PAID""" \temp\paid.xml
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == ""VOIDED""" \temp\voided.xml
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14)" \temp\all.xml
运行批处理给出:
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == "AUTHORISED"
\temp\authorised.xml
pages: 1. invoices: 32
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == "DELETED"
\temp\deleted.xml
pages: 1. invoices: 0
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == "DRAFT"
\temp\draft.xml
pages: 1. invoices: 2
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == "PAID"
\temp\paid.xml
pages: 34. invoices: 3339
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == "VOIDED"
\temp\voided.xml
pages: 1. invoices: 26
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14)
\temp\all.xml
pages: 34. invoices: 3339
请注意,“all.xml”输出存储了 3339 张发票。“paid.xml”还存储了 3339 张发票。如果您进行算术运算,您会注意到作废、付款、草稿、删除和授权的数字加起来大于 3339。
这有点奇怪。有任何想法吗?
附言
这也很奇怪,在 all.xml 中,有一些项目的 DueDate 超出了请求的范围,例如
<DueDate>7/22/2016 12:00:00 AM</DueDate>
聚苯乙烯
也许我只需要绕过 Xero 过滤并自己做,即
var everything = new System.Collections.Generic.List<Xero.Api.Core.Model.Invoice>();
...
foreach (Invoice I in invoices)
{
everything.Add(I);
}
...
IEnumerable<Invoice> some = everything
.Where(i => i.DueDate >= new DateTime(2017, 08, 01)
&& i.DueDate < new DateTime(2017, 09, 13)
&& i.Status == Api.Core.Model.Status.InvoiceStatus.Paid);