public static List <SampleType> datasetSamples(ValuesDataSet ds, string valuesWhereClause) { /* generate a list * create a distinct DataSet * - new data view * - set filter (no nulls) * - use toTable with unique to get unique list * foreach to generate qualifiers * */ string COLUMN = "SampleID"; string TABLENAME = "Sample"; List <SampleType> list = new List <SampleType>(); try { DataView view = new DataView(ds.DataValues); view.RowFilter = valuesWhereClause; DataTable ids = view.ToTable(TABLENAME, true, new string[] { COLUMN }); foreach (DataRow r in ids.Rows) { try { //Object aId = r[COLUMN]; if (r[COLUMN] == DBNull.Value) { continue; } int?aId = Convert.ToInt32(r[COLUMN]); // edit here ValuesDataSet.SamplesRow samples = ds.Samples.FindBySampleID((int)aId.Value); if (samples != null) { SampleType t = new SampleType(); t.sampleID = samples.SampleID; t.sampleIDSpecified = true; t.labSampleCode = samples.LabSampleCode; if (!String.IsNullOrEmpty(samples.SampleType)) { t.sampleType = samples.SampleType; } LabMethodType labMethod = new LabMethodType(); labMethod.labMethodName = samples.LabMethodName; labMethod.labName = samples.LabName; labMethod.labOrganization = samples.LabOrganization; labMethod.labCode = samples.LabSampleCode; labMethod.labMethodDescription = samples.LabMethodDescription; if (!samples.IsLabMethodLinkNull()) { labMethod.labMethodLink = samples.LabMethodLink; } t.labMethod = labMethod; list.Add(t); } } catch (Exception e) { log.Error("Error generating a Samples " + r.ToString() + e.Message); } } return(list); } catch (Exception e) { log.Error("Error generating a Samples " + e.Message); // non fatal exceptions return(null); } }
public static IEnumerable <ValueSingleVariable> dataset2ValuesList(ValuesDataSet ds, VariableParam variable, int?VariableId, VariablesDataset variablesDs) { Boolean variableIsCategorical = false; VariableInfoType variableInfoType = ODvariables.GetVariableByID(VariableId, variablesDs); if (variableInfoType != null && variableInfoType.dataType.Equals("Categorical")) { variableIsCategorical = true; } /* logic * if there is a variable that has options, then get a set of datarows * using a select clause * use an enumerator, since it is generic * */ IEnumerator dataValuesEnumerator; // = ds.DataValues.GetEnumerator(); ValuesDataSet.DataValuesRow[] dvRows; // if we find options, we need to use this. String select = OdValuesCommon.CreateValuesWhereClause(variable, VariableId); if (select.Length > 0) { dvRows = (ValuesDataSet.DataValuesRow[])ds.DataValues.Select(select.ToString()); dataValuesEnumerator = dvRows.GetEnumerator(); } else { dataValuesEnumerator = ds.DataValues.GetEnumerator(); } while (dataValuesEnumerator.MoveNext()) { ValuesDataSet.DataValuesRow aRow = (ValuesDataSet.DataValuesRow)dataValuesEnumerator.Current; ValueSingleVariable tsTypeValue = new ValueSingleVariable(); Boolean goodValue = false; try { tsTypeValue.dateTime = Convert.ToDateTime(aRow.DateTime); DateTime temprealdate; TimeSpan zone = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now); Double offset = Convert.ToDouble(aRow.UTCOffset); if (zone.TotalHours.Equals(offset)) { temprealdate = DateTime.SpecifyKind(Convert.ToDateTime(aRow.DateTime), DateTimeKind.Local); } else { temprealdate = DateTime.SpecifyKind(Convert.ToDateTime(aRow.DateTime), DateTimeKind.Utc); temprealdate = temprealdate.AddHours(offset); } temprealdate = Convert.ToDateTime(aRow.DateTime); tsTypeValue.dateTime = temprealdate; tsTypeValue.dateTimeUTC = aRow.DateTimeUTC; tsTypeValue.dateTimeUTCSpecified = true; tsTypeValue.timeOffset = OffsetDoubleToHoursMinutesString(aRow.UTCOffset); //tsTypeValue.dateTime = new DateTimeOffset(temprealdate); //tsTypeValue.dateTimeSpecified = true; if (string.IsNullOrEmpty(aRow.Value.ToString())) { continue; } else { tsTypeValue.Value = Convert.ToDecimal(aRow.Value); } try { tsTypeValue.censorCode = aRow.CensorCode; if (!aRow.IsOffsetTypeIDNull()) { //tsTypeValue.offsetTypeID = aRow.OffsetTypeID.ToString(); tsTypeValue.offsetTypeCode = aRow.OffsetTypeID.ToString(); // HIS-97 moved to OffsetUnitsType // ValuesDataSet.OffsetTypesRow off = // ds.OffsetTypes.FindByOffsetTypeID(aRow.OffsetTypeID); // ValuesDataSet.UnitsRow offUnit = ds.Units.FindByUnitsID(off.OffsetUnitsID); // tsTypeValue.offsetUnitsCode = offUnit.UnitsID.ToString(); // tsTypeValue.offsetUnitsAbbreviation = offUnit.UnitsAbbreviation; } // offset value may be separate from the units... anticpating changes for USGS if (!aRow.IsOffsetValueNull()) { tsTypeValue.offsetValue = aRow.OffsetValue; tsTypeValue.offsetValueSpecified = true; } ValuesDataSet.MethodsRow meth = ds.Methods.FindByMethodID(aRow.MethodID); // tsTypeValue.methodID = aRow.MethodID.ToString(); tsTypeValue.methodCode = aRow.MethodID.ToString(); // qualifiers if (!aRow.IsQualifierIDNull()) { ValuesDataSet.QualifiersRow qual = ds.Qualifiers.FindByQualifierID(aRow.QualifierID); if (qual != null) { tsTypeValue.qualifiers = qual.QualifierCode; } } // quality control level ValuesDataSet.QualityControlLevelsRow qcl = ds.QualityControlLevels.FindByQualityControlLevelID(aRow.QualityControlLevelID); string qlName = qcl.Definition.Replace(" ", ""); tsTypeValue.qualityControlLevelCode = qcl.QualityControlLevelCode; //if (!String.IsNullOrEmpty(qlName)) //{ // tsTypeValue.qualityControlLevel = qlName; //} // tsTypeValue.sourceID = aRow.SourceID.ToString(); tsTypeValue.sourceCode = aRow.SourceID.ToString(); if (!aRow.IsSampleIDNull()) { //tsTypeValue.sampleID = aRow.SampleID.ToString(); ValuesDataSet.SamplesRow lsc = ds.Samples.FindBySampleID(aRow.SampleID); tsTypeValue.labSampleCode = lsc.LabSampleCode; } // categorical if (variableIsCategorical && VariableId.HasValue) { tsTypeValue.codedVocabularyTerm = ODvariables.GetCategoryTerm(VariableId.Value, tsTypeValue.Value, variablesDs); if (!String.IsNullOrEmpty(tsTypeValue.codedVocabularyTerm)) { tsTypeValue.codedVocabulary = true; } tsTypeValue.codedVocabularySpecified = true; } } catch (Exception e) { log.Debug("Error generating a value " + e.Message); // non fatal exceptions } goodValue = true; } catch (Exception e) { goodValue = false; // If there is an error, we do not add it. } if (goodValue) { yield return(tsTypeValue); } } }