Lookup tables

General syntax

lookup_table.Values.Query(x=>x.var_to_query == 1)

Item Explanation
lookup_table Name of the lookup table
Keys, Values Component of the lookup table (dictionary) used to query
x Anonymous variable that captures the queried content
x.var_to_query Query variable(s); instance of that (those) variable(s)

Common queries

  • Any
    • Explanation: At least one element of the collection meets the query criteria.
    • Result: true/false.
  • FirstOrDefault
    • Explanation: Find the first element that meets the criteria.
    • Result: First element found or default (null)

Use case 1: Filter units

Use case 1: Filter units


/* valid consumption units for this product */

// if the product exists in the table...
valid_unit_sizes_Cereals.Values.Any(x=>
    x.product_code == @rowcode
) 

?

// ...then only show units found in the lookup table
valid_unit_sizes_Cereals.Values.Any(x=>
    x.product_code == @rowcode &&   // product
    x.unit_code == @optioncode      // unit
)

:

// otherwise, show all units
true

Use case 2: Validations

Use case 2: Validations

// Check for outliers using prior data
(new Func<bool>(() =>
{
    // find the lower and upper bounds of the item for that unit
    var bounds = agc04bounds.Values.FirstOrDefault(x => x.unit == (double) ag_c04b);
    if (bounds==null) return true;  // Return true if no such unit
    
    // determine whether the plot area is within the
    // 5th and 95th percentile
    return (ag_c04a.InRange(bounds.lower,bounds.upper));
})).Invoke()