byrmol Blog

Garbage

DataTable Relational Operators in C# - Divide Method

This DIVIDE method has no equivalent in TSQL.

Essential the DIVIDE operator is the inverse of Product (or CROSS JOIN in SQL Server speak).
ie: A PRODUCT B DIVIDE B = A

In this method, instead of doing manual calculations, I decided to leverage existing methods using the following formulae.
DIVIDE = PROJECT(A,Column) DIFFERENCE ((PROJECT(A,Column) PRODUCT B) DIFFERENCE A)[Column]

There is only the one signature for this method.
This version uses the DISTINCT method instead of PROJECT due to reasons outlined in the previous post DISTINCT & apology
The inline comments are self explanatory and follow the above formulae…

public static DataTable Divide(DataTable DEND, DataTable DOR, DataColumn BY)

{

      //First Create Distinct DEND table projected over BY column

      DataTable distinct = SQLOps.Distinct(DEND, BY);

      //Product of distinct and DOR

      DataTable product = SQLOps.Product(distinct,DOR);

      //Difference of product and DEND

      DataTable difference = SQLOps.Difference(product, DEND);

      //Project over BY column

      difference = SQLOps.Project(difference,new DataColumn[]{difference.Columns[BY.ColumnName]});

      //Difference of distinct AND difference

      DataTable table = SQLOps.Difference(distinct,difference);

     

      table.TableName = "Divide";

      return table;

}