简体   繁体   English

通过C#的Dynamics AX 2012 AIF QueryService-多个表之间的关系和JoinMode的使用

[英]Dynamics AX 2012 AIF QueryService through C# - Relations and JoinMode usage between multiple tables

I want to create a complex User defined AIF Query service by using Visual studio and writing code for query service in C#. 我想通过使用Visual Studio并在C#中为查询服务编写代码来创建复杂的用户定义AIF查询服务。 I am using two custom tables among which one is header and one is line table. 我正在使用两个自定义表,其中一个是标题,一个是行表。 I have added fields in the query by using both the tables and applies proper ranges as well and it works fine. 我通过使用两个表在查询中添加了字段,并且也应用了适当的范围,并且工作正常。 Now I want to make a relation between these two tables So I can fetch data based on relation. 现在,我想在这两个表之间建立关系,以便我可以基于关系来获取数据。

Please help me out with how to make a relation between multiple tables in AIF query service. 请帮助我了解如何在AIF查询服务中的多个表之间建立关系。

query.DataSources = new QueryDataSourceMetadata[2];    
query.Name = "AXCustomerInfo";    
QueryDataSourceMetadata custTableDS = new QueryDataSourceMetadata();    
custTableDS.Name = "CustTable";    
custTableDS.Table = "CustTable";    
custTableDS.Enabled = true;    
query.DataSources[0] = custTableDS;    
custTableDS.DynamicFieldList = false;    
custTableDS.Fields = new QueryDataFieldMetadata[1];  
QueryDataFieldMetadata accountNum;    
accountNum = new QueryDataFieldMetadata();    
accountNum.FieldName = "AccountNum";
accountNum.SelectionField = SelectionField.Database;    
custTableDS.Fields[0] = accountNum;
custTableDS.HasRelations = true;
custTableDS.JoinMode =JoinMode.InnerJoin;
QueryDataSourceMetadata dirPartyTableDS = new QueryDataSourceMetadata();
dirPartyTableDS.Name = "DirPartyTable";
dirPartyTableDS.Table = "DirPartyTable";    
dirPartyTableDS.Enabled = true;    
query.DataSources[1] = dirPartyTableDS;    
dirPartyTableDS.DynamicFieldList = false;    
dirPartyTableDS.Fields = new QueryDataFieldMetadata[1];        
QueryDataFieldMetadata name;    
name = new QueryDataFieldMetadata();    
name.FieldName = "Name";    
name.SelectionField = SelectionField.Database;    
dirPartyTableDS.Fields[0] = name;    
dirPartyTableDS.HasRelations = false;        
result = client.ExecuteQuery(query, ref paging);
foreach (DataRow row in result.Tables[0].Rows)    
{    
    Console.WriteLine(String.Format("{0}", row[0]));
    foreach (DataRow row1 in result.Tables[1].Rows)
    {    
        Console.WriteLine(String.Format("{0}", row1[0]));
    }
}

See how to add multiple datasources to a query . 了解如何向查询添加多个数据源

Either: 要么:

  • Set the Relations property to Yes on the child data source. 在子数据源上将“关系”属性设置为“是”。

or: 要么:

  • Add a relation by doing the following: 通过执行以下操作添加关系:
    1. Set the Relations property to No on the child data source. 在子数据源上将“关系”属性设置为“否”。
    2. Right-click the Relations node, and then click New Relation. 右键单击“关系”节点,然后单击“新建关系”。
    3. Select a field from the parent data source in the Field property. 从“字段”属性中的父数据源中选择一个字段。
    4. Select a field from the child data source in the RelatedField property. 从子数据源的RelatedField属性中选择一个字段。
    5. Save all modifications. 保存所有修改。

You can then access the data of the query using OData Query Service . 然后,您可以使用OData Query Service访问查询的数据。

Also see this question . 也看到这个问题

Gaurav, 高拉夫

In your code, you are not creating a hierarchical structure of data sources. 在您的代码中,您没有创建数据源的层次结构。 Hence relations will not work. 因此,关系将不起作用。 You will get errors like : You can not add relation to root data source. 您将得到类似以下错误:您无法向根数据源添加关系。 I already shared my response on AX community. 我已经在AX社区上分享了我的反馈。 Here is the link: https://community.dynamics.com/ax/f/33/p/212065/573674#573674 这是链接: https : //community.dynamics.com/ax/f/33/p/212065/573674#573674

This will work. 这将起作用。

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

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