3
我正在使用Dynamics AX 2012 R2查詢服務,並需要在CustTable的modifiedDateTime字段上過濾(設置範圍)。我正在創建一個QueryDataRangeMetadata對象並設置其屬性。我可以正確過濾整數值,但不是DateTimes。如何在AX查詢服務中格式化DateTime比較值
我能夠發現比較運算符實際上嵌入了該值。我已經測試了這個整數字段,它確實有效,但我一直無法弄清楚如何格式化日期時間值,以便正確評估。下面的代碼不起作用。該範圍將被忽略,並返回CustTable中的所有記錄。
public static void RangeTest()
{
var client = new QueryServiceClient();
var dataSource = new QueryDataSourceMetadata
{
Table = "CustTable",
Name = "CustTable",
HasRelations = false,
Enabled = true,
DynamicFieldList = true // get all fields
};
var range = new QueryDataRangeMetadata
{
TableName = "CustTable",
FieldName = "modifiedDateTime",
Value = ">2013-02-05T21:17:33Z", // <-- ISSUE: notice the operator with the value!
Enabled = true
};
dataSource.Ranges = new QueryRangeMetadata[] { range };
var sort = new QueryDataOrderByMetadata
{
DataSource = "CustTable",
FieldName = "modifiedDateTime",
SortOrder = SortOrder.Ascending
};
var query = new QueryMetadata
{
QueryType = QueryType.Join,
DataSources = new[] { dataSource },
OrderByFields = new QueryOrderByMetadata[] { sort }
};
Paging paging = null;
var dataSet = client.ExecuteQuery(query, ref paging);
Console.WriteLine(dataSet.Tables[0].Rows.Count);
}
我自己也嘗試沒有成功這些格式的變化:
Value = ">2013-02-05 21:17:33"
Value = ">2013-02-05T9:17:33"
Value = ">'2013-02-05T9:17:33'"
Value = ">2013-02-05T21:17:33Z"
任何人都知道的日期時間的格式應該是在這種情況下?
如果用戶從設置中更改日期格式,該怎麼辦。您的代碼將停止運行....您是否找到其他解決方案? – 2014-02-10 19:08:14
@SaboorAwan:OP中的代碼片段,答案是硬編碼的,但爲了簡單起見。對於生產,您可以使用用戶的文化進行格式化。更好的是,您應該使用UTC日期時間和文字:http://msdn.microsoft.com/en-us/library/cc597805.aspx – 2014-02-11 16:37:56