简体   繁体   English

通过C#的Dynamics AX 2012 QueryService示例-关系和JoinMode的用法

[英]Example for Dynamics AX 2012 QueryService through C# - Relations and JoinMode usage

Trying to access AX 2012 QueryService through c# application and would like to get CustomerID and Customername fields only. 尝试通过c#应用程序访问AX 2012 QueryService,并且仅希望获取CustomerID和Customername字段。 Problem is, CustomerID is in CustTable and CustomerName is in DirPartyTable. 问题是,CustomerID在CustTable中,而CustomerName在DirPartyTable中。

Any help to article or code samples would be appreciated. 对文章或代码示例的任何帮助将不胜感激。

  1. Reference to QueryDataSourceMetadata to put join in these tables and get data. 引用QueryDataSourceMetadata以将联接放入这些表中并获取数据。
  2. What are the options and when to use Relations and JoinMode? 有哪些选项以及何时使用Relations和JoinMode?

I searched in MSDN and it just lists the property names and methods and not much help in the form of code samples. 我在MSDN中进行了搜索,它仅列出了属性名称和方法,而以代码示例的形式没有太多帮助。

According to my knowledge, you put in the two tables you want to query into your QueryDataSourceMetadata. 据我所知,您将要查询的两个表放入QueryDataSourceMetadata中。 The resulting dataset should contain both tables. 结果数据集应包含两个表。 (Example code for creating a QueryDataSourceMetadataObject is here: http://msdn.microsoft.com/EN-US/library/gg844682.aspx ) (用于创建QueryDataSourceMetadataObject的示例代码在此处: http : //msdn.microsoft.com/EN-US/library/gg844682.aspx

The property you need, for the tables to be joined, is ReturnFlatDataSet. 对于要连接的表,您需要的属性是ReturnFlatDataSet。 (this may help: http://msdn.microsoft.com/EN-US/library/gg841671.aspx ) (这可能会有所帮助: http : //msdn.microsoft.com/EN-US/library/gg841671.aspx

Hope i could help you or point you in the right direction! 希望我能为您提供帮助或为您指明正确的方向!

Its too late to answer and I think you already might have found some solutions. 现在回答还为时已晚,我想您可能已经找到了一些解决方案。 Anyway here is the link to my response on AX community: 无论如何,这是我对AX社区的回应的链接:

https://community.dynamics.com/ax/f/33/p/212065/573674#573674 https://community.dynamics.com/ax/f/33/p/212065/573674#573674


I found 2 ways to add the relations from c#. 我发现了两种从c#添加关系的方法。 I have commented the first approach based on AOT table relation. 我已经评论了基于AOT表关系的第一种方法。 In the code below QueryServiceReference is the service reference name in VS. 在下面的代码中,QueryServiceReference是VS中的服务引用名称。 You can remove it in all the lines if you have only QueryService reference but no MetaDataService reference. 如果只有QueryService引用但没有MetaDataService引用,则可以在所有行中将其删除。 Here is the code : 这是代码:

query.DataSources = new QueryServiceReference.QueryDataSourceMetadata[1];

// Set the properties on Customers data source.
 customerDataSource = new QueryServiceReference.QueryDataSourceMetadata();
 customerDataSource.Name = "Customers";
 customerDataSource.Enabled = true;
 customerDataSource.FetchMode = QueryServiceReference.FetchMode.OneToOne;
 customerDataSource.Table = "CustTable";
 //customerDataSource.DynamicFieldList = false;

query.DataSources[0] = customerDataSource;


 QueryServiceReference.QueryDataSourceMetadata dirPartyTableDataSource = new QueryServiceReference.QueryDataSourceMetadata();
 dirPartyTableDataSource.Name = "DirPartyTable";
 dirPartyTableDataSource.Table = "DirPartyTable";
 dirPartyTableDataSource.Enabled = true;
 dirPartyTableDataSource.DynamicFieldList = true;


 customerDataSource.DataSources = new QueryServiceReference.QueryDataSourceMetadata[1] { dirPartyTableDataSource };
 QueryServiceReference.QueryRelationMetadata relation = new QueryServiceReference.QueryRelationMetadata();

 //this is also one way of setting the relation 
 //relation.JoinRelation = "DirPartyTable_FK"; //table relation defined in AOT
 //relation.JoinDataSource = customerDataSource.Name; //parent datasource name

relation.Table = "CustTable";//Parent table
 relation.Field = "Party"; 
 relation.RelatedTable = "DirPartyTable"; // child table
 relation.RelatedField = "RecId";
 relation.JoinDataSource = customerDataSource.Name; 
 dirPartyTableDataSource.Relations = new QueryServiceReference.QueryRelationMetadata[1] { relation };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM