/// <summary> /// Retrieves the units of measure for the given unit identifiers. /// If no unit identifiers are provided then all the supported units of measure are retrieved. /// </summary> /// <param name="request">The units of measure request.</param> /// <returns> /// A unit of measure for the symbol. /// </returns> private EntityDataServiceResponse <UnitOfMeasure> GetUnitsOfMeasure(GetUnitsOfMeasureDataRequest request) { ThrowIf.Null(request, "request"); ThrowIf.Null(request.UnitIds, "request.UnitIds"); ThrowIf.Null(request.QueryResultSettings, "request.QueryResultSettings"); PagedResult <UnitOfMeasure> results; // Default query to retrieve all units of measure. var query = new SqlPagedQuery(request.QueryResultSettings) { From = GetUnitsOfMeasureFunctionName, Aliased = true, OrderBy = SymbolColumnName, }; query.Parameters["@LanguageId"] = request.RequestContext.LanguageId; // Update query when only one unit of measure is retrieved. if (request.UnitIds.Count() == 1) { query.Where = string.Format("{0} = @UnitId", SymbolColumnName); query.Parameters["@UnitId"] = request.UnitIds.FirstOrDefault(); } // Update query when multiple units of measure are retrieved. if (request.UnitIds.HasMultiple()) { IEnumerable <string> distinctUnitOfMeasure = request.UnitIds.Distinct(StringComparer.OrdinalIgnoreCase); using (StringIdTableType type = new StringIdTableType(distinctUnitOfMeasure, SymbolColumnName)) { query.Parameters["@TVP_UNITIDTABLETYPE"] = type; // Query execution for retrieving multiple units. results = this.ExecuteQuery(query, request.RequestContext); } } else { // Query execution for retrieving single or all the units. results = this.ExecuteQuery(query, request.RequestContext); } return(new EntityDataServiceResponse <UnitOfMeasure>(results)); }
/// <summary> /// Round quantity for the given unit of measure. /// </summary> /// <param name="request">Service request.</param> /// <returns>Rounded value.</returns> private GetRoundQuantityServiceResponse RoundQuantity(GetRoundQuantityServiceRequest request) { var unitsOfMeasureIds = new List <string> { request.UnitOfMeasure }; var getUnitsOfMeasureByUnitIdDataRequest = new GetUnitsOfMeasureDataRequest(unitsOfMeasureIds, QueryResultSettings.SingleRecord); UnitOfMeasure uom = request.RequestContext.Runtime.Execute <EntityDataServiceResponse <UnitOfMeasure> >(getUnitsOfMeasureByUnitIdDataRequest, request.RequestContext).PagedEntityCollection.FirstOrDefault(); int decimalPrecision = DefaultRoundingPrecision; if (uom != null) { decimalPrecision = uom.DecimalPrecision; } string roundedString = request.Value.ToString(string.Format("N{0}", decimalPrecision)); return(new GetRoundQuantityServiceResponse(Convert.ToDecimal(roundedString))); }