title: Dynamics AX 2012 AIF QueryService through C# - Relations and JoinMode usage between multiple tables
body: <p>I want to create a complex User defined AIF Query service by using Visual studio and writing code for query service in C#. 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.</p>
  
  <p>Please help me out with how to make a relation between multiple tables in AIF query service.</p>
  
  <pre><code>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]));
      }
  }
  </code></pre>