private static async Task <bool> UpdateDatabaseMeasurementsWithMeasurementData(ICollection <Measurement> measurements) { using (DescDBContext dBContext = new DescDBContext()) { foreach (var measurement in measurements) { var measurementExists = await dBContext.MeasurementDescriptions .Where(x => x.SectionInfo.ID == measurement.SectionInfo.ID) .Where(x => x.TextID == measurement.TextID) .Where(x => x.TestNumber == measurement.TestNumber) .Where(x => x.StartOffset == measurement.StartOffset && x.EndOffset == measurement.EndOffset) .FirstOrDefaultAsync().ConfigureAwait(true); if (measurementExists != null) { foreach (var columnValuePair in measurement.MeasurementData) { if (!measurementExists.MeasurementData.Any(x => x.ColumnName == columnValuePair.ColumnName)) { measurementExists.MeasurementData.Add(columnValuePair); } } dBContext.Update(measurement); await dBContext.SaveChangesAsync(); //dBContext.u } } } return(true); }
public static async Task <bool> CheckForSectionAsync(DescDBContext dBContext, SectionInfo section) { bool recordExists; if (section.ID != 0) { recordExists = await dBContext.Sections .Where(x => x.ID == section.ID) .AnyAsync().ConfigureAwait(true); } else if (section.ParentTextID != "") { recordExists = await dBContext.Sections .Where(x => x.ParentTextID == section.ParentTextID) .AnyAsync().ConfigureAwait(true); } else if (section.ID == 0) { recordExists = await dBContext.Sections .Where(x => section.Equals(x)) .AnyAsync().ConfigureAwait(true); } else { return(false); } return(recordExists); }
public static async Task <bool> AddSectionsToDatabaseAsync(DescDBContext dBContext, ICollection <SectionInfo> sections) { foreach (var section in sections) { var exists = await CheckForSectionAsync(dBContext, section).ConfigureAwait(true); if (exists) { sections.Remove(section); } } try { using (DescDBContext dbContext = new DescDBContext()) { dbContext.AddRange(sections); await dbContext.SaveChangesAsync().ConfigureAwait(true); return(true); } } catch (Exception) { throw new Exception(string.Format("Error adding sections to database")); } }
public static void SendMeasurementsToDatabase(Dictionary <int, Measurement> MeasurementCache) { _ = MeasurementCache ?? throw new ArgumentNullException(nameof(MeasurementCache)); DescDBContext context = null; try { context = new DescDBContext(); //context.Configuration.AutoDetectChangesEnabled = false; int count = 0; var measurementList = MeasurementCache.Values.Where(x => x.LithologicSubID.HasValue); for (int i = 0; i < measurementList.Count(); i = i + 100) { var entityToInsert = measurementList.Skip(i).Take(100); count = 100; context = AddMeasurementsToContext(context, entityToInsert, count, 100, true); } context.SaveChanges(); } finally { if (context != null) { context.Dispose(); } } }
/// <summary> /// Attaches the corresponding descriptions for a measurement interval from the database to the measurement. /// </summary> /// <param name="dbContext">A DESCDatabase context</param> /// <param name="measurement">A measurement interval</param> /// <param name="columnNames">The description columns to return</param> /// <returns></returns> public static async Task <Measurement> MatchDescriptionForMeasurementAsync(DescDBContext dbContext, Measurement measurement, [Optional] ICollection <string> columnNames) { measurement.SectionInfo = measurement.SectionInfo ?? throw new Exception("Measurement does not have Section information"); SectionInfo section = await dbContext.Sections.Where(x => measurement.SectionInfo.Equals(x)) .FirstOrDefaultAsync().ConfigureAwait(true); if (section == null) { return(measurement); } var x = dbContext.LithologicDescriptions .Where(description => description.SectionInfo.Equals(measurement.SectionInfo)) .Where(description => description.StartOffset <= measurement.StartOffset && description.EndOffset >= measurement.EndOffset); if (columnNames.Any()) { x = x.Include(description => description.DescriptionColumnValues.Where(columnValuPair => columnNames.Contains(columnValuPair.ColumnName)) ); } measurement.Descriptions = x.Select(g => g).ToHashSet(); return(measurement); }
public static void SendLithologiesToDatabase(Dictionary <string, LithologicDescription> LithologyCache) { _ = LithologyCache ?? throw new ArgumentNullException(nameof(LithologyCache)); DescDBContext context = null; try { context = new DescDBContext(); //context.Configuration.AutoDetectChangesEnabled = false; int count = 0; var descriptionList = LithologyCache.Values.Where(x => !string.IsNullOrEmpty(x.LithologicID)); // context.AddRange(descriptionList); for (int i = 0; i < descriptionList.Count(); i = i + 100) { var entityToInsert = descriptionList.Skip(i).Take(100).ToList(); count = 100; context = AddLithologiesToContext(context, entityToInsert, count, 100, true); } context.SaveChanges(); } finally { if (context != null) { context.Dispose(); } } }
public static async Task <bool> CheckForDescriptionAsync(DescDBContext dBContext, LithologicDescription description) { bool recordExists = await dBContext.LithologicDescriptions .Where(x => x.SectionInfo.ID == description.SectionInfo.ID) .Where(x => x.DescriptionReport == description.DescriptionReport) //Report names may change depending on how we name the files, .Where(x => x.StartOffset == description.StartOffset && x.EndOffset == description.EndOffset) .AnyAsync().ConfigureAwait(true); return(recordExists); }
public static async Task <ICollection <string> > GetUniquePrincipalLithologies() { using (DescDBContext dbContext = new DescDBContext()) { return(await dbContext.DescriptionColumnValuePairs .Where(x => x.CorrectedColumnName == "Principal Lithology") .Select(x => x.Value) .Distinct() .ToListAsync().ConfigureAwait(true)); } }
public static async Task <ICollection <string> > GetColumnNamesWhichContainsString(string lookupSet) { using (DescDBContext dbContext = new DescDBContext()) { return(await dbContext.DescriptionColumnValuePairs .Where(x => x.ColumnName.Contains(lookupSet, StringComparison.CurrentCulture)) .Select(x => x.ColumnName) .Distinct() .ToListAsync().ConfigureAwait(true)); } }
public static void AttemptAtSQLRaw() { List <LithologicDescription> IrregularOffsets = new List <LithologicDescription>(); using (DescDBContext dbContext = new DescDBContext()) { IrregularOffsets = dbContext.LithologicDescriptions .FromSqlRaw(@"SELECT * FROM [DESCDatabase].[dbo].[LithologicDescriptions] t1 WHERE t1.StartOffset > t1.EndOffset") .ToList(); } }
private static DescDBContext AddMeasurementsToContext(DescDBContext context, IEnumerable <Measurement> measurements, int count, int commitCount, bool recreateContext) { context.AddRange(measurements); //Detach all child records which already have been added otherwise I get the following error // Cannot insert explicit value for identity column in table ... when IDENTITY_INSERT is set to OFF. //Measurements can only get descriptions and subintervals which have already been loaded foreach (var measurement in measurements) { //try detaching only the subinterval // context.Entry(measurement.LithologicSubinterval).State = Microsoft.EntityFrameworkCore.EntityState.Detached; //context.Entry(measurement.LithologicDescription).State = Microsoft.EntityFrameworkCore.EntityState.Detached; context.Entry(measurement.LithologicDescription).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; foreach (LithologicSubinterval subInterval in measurement.LithologicDescription.LithologicSubintervals) { if (measurement.LithologicSubinterval.ID != 0) { context.Entry(subInterval).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; } } if (measurement.LithologicSubinterval.ID != 0) { context.Entry(measurement.LithologicSubinterval).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; } //DescriptionColumnValuePairs are tracked and accessed via the measurement's LithologicSubinterval property foreach (DescriptionColumnValuePair entry in measurement.LithologicDescription.DescriptionColumnValues) { context.Entry(entry).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; } if (measurement.SectionInfo.ID != 0) { context.Entry(measurement.SectionInfo).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; } } if (count % commitCount == 0) { context.SaveChanges(); if (recreateContext) { context.Dispose(); context = new DescDBContext(); } } return(context); }
public static async Task <bool> AddDescriptionsToDataBaseAsync(DescDBContext dbContext, ICollection <LithologicDescription> descriptions) { try { dbContext.AttachRange(descriptions); await dbContext.SaveChangesAsync(); return(true); } catch (Exception ex) { throw new Exception("Error adding descriptions to database context."); } }
/// <summary> /// Gets a collection of LithologicDescriptions from a corrected file. /// </summary> /// <param name="filename"></param> /// <param name="columnIdentifiers"></param> /// <returns></returns> public static async Task <ICollection <LithologicDescription> > GetDescriptionsFromFileAsync(string filename, [Optional] IntervalHierarchyNames columnIdentifiers) { columnIdentifiers = columnIdentifiers ?? new IntervalHierarchyNames() { Expedition = "Expedition_VP", Site = "Site_VP", Hole = "Hole_VP", Core = "Core_VP", Type = "Type_VP", Section = "Section_VP", Half = "SectionHalf_VP", TopOffset = "TopOffset_VP", BottomOffset = "BottomOffset_VP", ArchiveTextID = "ArchiveSectionTextID_VP", WorkingTextID = "WorkingSectionTextID_VP", ParentTextID = "ParentSectionTextID_VP" }; IODPDataTable iODPDataTable = Importer.ImportDataTableFromFile(filename, columnIdentifiers); ICollection <LithologicDescription> descriptions = new HashSet <LithologicDescription>(); using (DescDBContext dbContext = new DescDBContext()) { try { foreach (DataRow row in iODPDataTable.DataTable.Rows) { SectionInfo section = new SectionInfo(Importer.GetHierarchyValuesFromDataRow(row, columnIdentifiers)); LithologicDescription description = new LithologicDescription(); // description.SectionInfo = section; description.SectionInfo = await DatabaseWorkflowHandler.GetSectionInfoFromDatabaseForIntervalAsync(dbContext, section).ConfigureAwait(true); description.LithologicID = row["LithologicID_VP"].ToString(); description.DataRow = row; description.DescriptionReport = row["Filename_VP"].ToString();; description.StartOffset = double.TryParse(row[columnIdentifiers.TopOffset].ToString(), out double startOffset) ? startOffset : -1; description.EndOffset = double.TryParse(row[columnIdentifiers.BottomOffset].ToString(), out double endOffset) ? endOffset : -1; descriptions.Add(description); } } catch (Exception) { throw new Exception("Error creating lithologic description from data row"); } } return(descriptions); }
public static async Task <bool> AddMeasurementsToDataBaseAsync(DescDBContext dbContext, ICollection <Measurement> measurements) { try { dbContext.AttachRange(measurements); await dbContext.SaveChangesAsync(); return(true); } catch (Exception ex) { Log.Warning(ex.Message); throw new Exception("Error adding measurements to database context."); } }
public static async Task <ICollection <Measurement> > GetMeasurementIDsForLithology(string principalLithology) { using (DescDBContext dbContext = new DescDBContext()) { return(await dbContext.LithologicDescriptions .Include(x => x.DescriptionColumnValues .Where(x => x.CorrectedColumnName == "Principal Lithology") .Where(x => x.Value == principalLithology)) .Include(x => x.LithologicSubintervals) .ThenInclude(x => x.Measurements .Where(x => x.InstrumentSystem == "GRA" || x.InstrumentSystem == "MAD")) .SelectMany(x => x.LithologicSubintervals) .SelectMany(x => x.Measurements) .Take(1000) .ToListAsync().ConfigureAwait(true)); } }
private static DescDBContext AddToContext <TEntity>(DescDBContext context, IEnumerable <TEntity> entities, int count, int commitCount, bool recreateContext) where TEntity : class { context.AddRange(entities); if (count % commitCount == 0) { context.SaveChanges(); if (recreateContext) { context.Dispose(); context = new DescDBContext(); } } return(context); }
public static async Task <bool> GetDescriptionsForMeasurment(string InstrumentSystem, ICollection <string> DescriptionColumns) { // ICollection<Measurement> measurements; using (DescDBContext dbContext = new DescDBContext()) { var measurements = await dbContext.MeasurementDescriptions .Include(x => x.LithologicSubintervals) .ThenInclude(x => x.LithologicDescription) .ThenInclude(x => x.DescriptionColumnValues.Where(x => DescriptionColumns.Contains(x.ColumnName))) .Where(x => x.InstrumentSystem == InstrumentSystem) .Take(100) .Select(x => x) .ToListAsync().ConfigureAwait(true); } return(true); }
/// <summary> /// Parses a file into Measurements, checks the database for existence, if records are new uploads to database. /// </summary> /// <param name="filename"></param> /// <param name="columnIdentifiers"></param> /// <returns></returns> private static async Task <bool> UploadMeasurementsFromFileToDatabaseAsync(string filename, [Optional] IntervalHierarchyNames columnIdentifiers) { var measurements = await MeasurementHandler.GetMeasurementsFromFileAsync(filename, columnIdentifiers).ConfigureAwait(true); ICollection <Measurement> measurementsToRemove = new HashSet <Measurement>(); using (DescDBContext dBContext = new DescDBContext()) { foreach (var measurement in measurements) { var measurementExists = await DatabaseWorkflowHandler.FindMeasurementInDatabase(dBContext, measurement).ConfigureAwait(true); if (measurementExists) { measurementsToRemove.Add(measurement); } } } foreach (var measurement in measurementsToRemove) { measurements.Remove(measurement); } if (measurements.Count == 0) { return(false); } bool isDataUploaded; foreach (var measurement in measurements) { measurement.MeasurementData.Clear(); //Not uploading measurement report data at the moment } using (DescDBContext dbContext = new DescDBContext()) { isDataUploaded = await DatabaseWorkflowHandler.AddMeasurementsToDataBaseAsync(dbContext, measurements).ConfigureAwait(true); } return(isDataUploaded); }
public static async Task <int> GetMeasurementIDAsync(DescDBContext dbContext, Measurement measurement) { var record = await dbContext.MeasurementDescriptions .Where(x => x.SectionInfo.ID == measurement.SectionInfo.ID) .Where(x => x.TextID == measurement.TextID) .Where(x => x.TestNumber == measurement.TestNumber) .Where(x => x.InstrumentSystem == measurement.InstrumentSystem) .Where(x => x.StartOffset == measurement.StartOffset && x.EndOffset == measurement.EndOffset) .FirstOrDefaultAsync().ConfigureAwait(true); if (record != null) { return(record.ID); } else { return(-1); } }
/// <summary> /// T /// </summary> /// <param name="descriptionsToTake">The number of records to create intervals for before the dbContext is disposed</param> /// <returns></returns> public static async Task <bool> CreateSubIntervalsForDescriptions(int descriptionsToTake) { //Do this expedition by expedition, after expedition measurements have been uploaded, this would only need to be called once try { using (DescDBContext dbContext = new DescDBContext()) { var descriptions = dbContext.LithologicDescriptions .Include(x => x.SectionInfo) .Include(x => x.LithologicSubintervals) .ThenInclude(x => x.SectionInfo) .Include(x => x.LithologicSubintervals) .ThenInclude(x => x.LithologicDescription) //The object graph, I want to add subintervals to a description, and add sectioninfo and description references to those subintervals // .Where(x => x.SectionInfo.Expedition == expedition) .Where(x => x.LithologicSubintervals.Any() == false) //The description should not have subintervals generated for it yet. // .Where (x=>x.SectionInfo.Expedition == expedition) .Where(x => x.EndOffset > x.StartOffset) //There are some descriptions that have incorrectly entered start and end offsets .Take(descriptionsToTake) .ToHashSet(); if (descriptions.Any() == false) { return(false); } foreach (var description in descriptions) { description.GenerateSubintervals(); } dbContext.AttachRange(descriptions); //I can use Attach, but the objects are being tracked presently, SaveChanges should work just fine dbContext.SaveChanges(); } } catch (Exception ex) { throw new Exception("Error generating subintervals for descriptions."); } return(true); }
public static async Task <bool> FindMeasurementInDatabase(DescDBContext dbContext, Measurement measurement) { //TODO: Throw an error if the measurement doesn't have a sectioninfo ID bool recordExists = await dbContext.MeasurementDescriptions .Where(x => x.SectionInfo.ID == measurement.SectionInfo.ID) .Where(x => x.TextID == measurement.TextID) .Where(x => x.TestNumber == measurement.TestNumber) .Where(x => x.StartOffset == measurement.StartOffset && x.EndOffset == measurement.EndOffset) .AnyAsync().ConfigureAwait(true); if (recordExists) { return(true); } else { return(false); } }
private static DescDBContext AddLithologiesToContext(DescDBContext context, IEnumerable <LithologicDescription> descriptions, int count, int commitCount, bool recreateContext) { context.AddRange(descriptions); //Detach all child records which already have been added otherwise I get the following error // Cannot insert explicit value for identity column in table 'Sections' when IDENTITY_INSERT is set to OFF. foreach (var description in descriptions) { if (description.SectionInfo.ID != 0) { context.Entry(description.SectionInfo).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; } foreach (LithologicSubinterval subInterval in description.LithologicSubintervals) { //Detach subintervals already added (and have non-zero primary keys) if (subInterval.ID != 0) { context.Entry(subInterval).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; } //Detach subinterval sectioninfo already added (and have non-zero primary keys) if (subInterval.SectionInfo.ID != 0) { context.Entry(subInterval.SectionInfo).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; } } } if (count % commitCount == 0) { context.SaveChanges(); if (recreateContext) { context.Dispose(); context = new DescDBContext(); } } return(context); }
private static async Task <bool> UploadDescriptionsFromFileToDatabaseAsync(string filename, [Optional] IntervalHierarchyNames columnIdentifiers) { var descriptions = await DescriptionHandler.GetDescriptionsFromFileAsync(filename, columnIdentifiers).ConfigureAwait(true); try { using (DescDBContext dBContext = new DescDBContext()) { foreach (var description in descriptions) { if (await DatabaseWorkflowHandler.CheckForDescriptionAsync(dBContext, description).ConfigureAwait(true)) { descriptions.Remove(description); } } } } catch (Exception ex) { throw ex; } if (descriptions.Count == 0) { return(false); } bool isDataUploaded; using (DescDBContext dbContext = new DescDBContext()) { isDataUploaded = await DatabaseWorkflowHandler.AddDescriptionsToDataBaseAsync(dbContext, descriptions).ConfigureAwait(true); } return(isDataUploaded); }
public static async Task <ICollection <Mashup> > GetMeasurementsWithDrillingDisturbances(string instrumentReport) { using (DescDBContext descDBContext = new DescDBContext()) { return(await descDBContext.MeasurementDescriptions .Where(x => x.InstrumentSystem == "GRA") .Include(x => x.LithologicSubintervals .Where(x => x.LithologicDescription .DescriptionColumnValues .Any(x => x.ColumnName == "Drilling disturbance type" || x.ColumnName == "Drilling disturbance intensity"))) .ThenInclude(x => x.LithologicDescription .DescriptionColumnValues .Where(x => x.ColumnName == "Drilling disturbance type" || x.ColumnName == "Drilling disturbance intensity")) .SelectMany(measurement => measurement.LithologicSubintervals, (measurement, subinterval) => new { measurement.ID, subinterval }) .SelectMany(x => x.subinterval.LithologicDescription.DescriptionColumnValues, (x, columnvalues) => new Mashup { MeasurementID = x.ID, LithologicSubID = x.subinterval.ID, ColumnValuePairID = columnvalues.ID, ColumnName = columnvalues.ColumnName, ColumnValue = columnvalues.Value }) .Where(x => x.ColumnName == "Drilling disturbance type" || x.ColumnName == "Drilling disturbance intensity") .Select(x => x) .ToListAsync().ConfigureAwait(true)); } }
/// <summary> /// Asynchronously gets a collection of IODP measurements from a .csv file. /// </summary> /// <param name="filename">The .csv file location</param> /// <param name="columnIdentifiers">Optional parameter which specifies the file's column names</param> /// <returns>A collection of measurements</returns> public static async Task <ICollection <Measurement> > GetMeasurementsFromFileAsync(string filename, [Optional] IntervalHierarchyNames columnIdentifiers) { columnIdentifiers = columnIdentifiers ?? new IntervalHierarchyNames() { Expedition = "Exp", Site = "Site", Hole = "Hole", Core = "Core", Type = "Type", Section = "Sect", Half = "A/W", TopOffset = "Offset (cm)", BottomOffset = "Offset (cm)", ArchiveTextID = "ArchiveSectionTextID_VP", WorkingTextID = "WorkingSectionTextID_VP", ParentTextID = "ParentSectionTextID_VP", SampleID = "Sample", TextID = "Text ID", TestNumber = "Test No.", }; //TODO: need to get this some other way string InstrumentSystem = Importer.GetFileNameWithoutExtension(filename); if (InstrumentSystem == "CARB" || InstrumentSystem == "ICP") { columnIdentifiers.TopOffset = "Top offset on section (cm)"; columnIdentifiers.BottomOffset = "Bot offset on section (cm)"; } IODPDataTable iODPDataTable = Importer.ImportDataTableFromFile(filename, columnIdentifiers); ICollection <Measurement> measurements = new HashSet <Measurement>(); try { foreach (DataRow row in iODPDataTable.DataTable.Rows) { IntervalHierarchyValues parsedValues = Importer.GetHierarchyValuesFromDataRow(row, columnIdentifiers); Measurement measurement = new Measurement(); measurement.SectionInfo = new SectionInfo(parsedValues); //Creating a SectionInfo here that will be used to find the one stored in the DB. measurement.DataRow = row; measurement.InstrumentReport = ""; measurement.InstrumentSystem = InstrumentSystem; measurement.TextID = parsedValues.TextID; measurement.TestNumber = parsedValues.TestNumber; measurement.StartOffset = double.TryParse(row[columnIdentifiers.TopOffset].ToString(), out double startOffset) ? startOffset : -1; measurement.EndOffset = double.TryParse(row[columnIdentifiers.BottomOffset].ToString(), out double endOffset) ? endOffset : -1; measurements.Add(measurement); } } catch (Exception) { throw new Exception("Error creating measurement from data row"); } using (DescDBContext dbContext = new DescDBContext()) { string[] expeditions = measurements.Select(x => x.SectionInfo.Expedition).Distinct().ToArray(); ICollection <SectionInfo> sections; try { sections = await DatabaseWorkflowHandler.GetAllSectionsFromDatabaseForExpeditionAsync(dbContext, expeditions).ConfigureAwait(false); } catch (Exception) { throw new Exception("Could not get sections from the database"); } foreach (var measurement in measurements) { measurement.SectionInfo = DatabaseWorkflowHandler.GetSectionInfoFromCollection(sections, measurement.SectionInfo); } return(measurements); } }
public static async Task <ICollection <Measurement> > GetDescriptionsForMeasurementAsync(DescDBContext dBContext, IEnumerable <Measurement> measurements, [Optional] ICollection <string> columnNames) { var userTasks = new List <Task <Measurement> >(); foreach (var measurement in measurements) { userTasks.Add(MatchDescriptionForMeasurementAsync(dBContext, measurement, columnNames)); } return(await Task.WhenAll(userTasks).ConfigureAwait(true)); }
public static async Task <ICollection <LithologicDescription>[]> GetDescriptionsWithinMeasurementIntervalAsync(DescDBContext dBContext, IEnumerable <Measurement> measurements, [Optional] ICollection <string> columnNames) { var userTasks = new List <Task <ICollection <LithologicDescription> > >(); foreach (var measurement in measurements) { userTasks.Add(GetDescriptionsWithinMeasurementIntervalAsync(dBContext, measurement, columnNames)); } return(await Task.WhenAll(userTasks).ConfigureAwait(true)); }
private static async Task <bool> GetMeasurementsForSubIntervals(int skip, int take) { Console.Clear(); using (DescDBContext dbContext = new DescDBContext()) { var measurements = dbContext.MeasurementDescriptions .Where(x => x.ID > 14221456 - 1) //Enable this to start attributing descriptions after a certain ID, it's useful if adding new measurements .Include(x => x.SectionInfo) .Include(x => x.LithologicSubintervals) .Skip(skip) .Take(take) .Select(x => x) .ToHashSet(); foreach (var measurement in measurements) { Console.WriteLine(string.Format("Measurement ID: {0}", measurement.ID.ToString())); List <LithologicSubinterval> matchingSubintervals; try { matchingSubintervals = await dbContext.LithologicSubintervals .Include(x => x.SectionInfo) .Where(subinterval => subinterval.SectionInfo.ID == measurement.SectionInfo.ID) .Where(description => (measurement.StartOffset >= description.StartOffset && measurement.StartOffset <= description.EndOffset) || //Finds an interval in which the measurement falls in part (measurement.EndOffset >= description.StartOffset && measurement.EndOffset <= description.EndOffset)) .ToListAsync().ConfigureAwait(true); } catch (Exception ex) { Console.WriteLine("Error finding subintervals for measurement"); continue; } if (matchingSubintervals.Count == 0) { continue; } try { foreach (var subinterval in matchingSubintervals) { //Add only if it doesn't contain the subinterval if (!measurement.LithologicSubintervals.Contains(subinterval)) { measurement.LithologicSubintervals.Add(subinterval); Console.WriteLine(string.Format("Added Subinterval ID: {0}", subinterval.ID.ToString())); } } } catch (Exception ex) { Console.WriteLine(string.Format("Error adding Subintervals for Measurement: {0}", measurement.ID.ToString())); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } } ; await dbContext.SaveChangesAsync().ConfigureAwait(true); } return(true); }
public static async Task <bool> GetMeasurementIDForMeasurementFile(string file, string exportFilePath) { ICollection <Measurement> measurements = await MeasurementHandler.GetMeasurementsFromFileAsync(file).ConfigureAwait(true); using (DescDBContext dBContext = new DescDBContext()) { foreach (var measurement in measurements) { measurement.ID = await DatabaseWorkflowHandler.GetMeasurementIDAsync(dBContext, measurement).ConfigureAwait(true); } } //Column Names HashSet <string> columns = measurements.First().MeasurementData.Select(x => x.ColumnName).ToHashSet(); //Check if all measurements have the same Columns foreach (var measurement in measurements) { var compareColumns = measurement.MeasurementData.Select(x => x.ColumnName).ToHashSet(); if (!columns.SetEquals(compareColumns)) { return(false); } } //TODO: Construct new Datatable using (DataTable dataTable = new DataTable()) { foreach (var column in columns) { dataTable.Columns.Add(column); } dataTable.Columns.Add("MeasurementID").SetOrdinal(0); int currentRow = 0; foreach (var measurement in measurements) { dataTable.ImportRow(measurement.DataRow); var row = dataTable.Rows[currentRow]; row.BeginEdit(); row["MeasurementID"] = measurement.ID; row.EndEdit(); currentRow++; } //AddID column //Export measurements to File Importer.ExportDataTableAsNewFile(exportFilePath, dataTable); } return(true); }
/// <summary> /// Asynchronously gets a SectionInfo from the databse for a given measurement or lithologic description interval /// </summary> /// <param name="dbContext">A DESCDatabase context</param> /// <param name="interval">An interval</param> /// <returns>A single SectionInfo</returns> public static async Task <SectionInfo> GetSectionInfoFromDatabaseForIntervalAsync(DescDBContext dbContext, SectionInfo section) { return(await dbContext.Sections.Where(record => record.Expedition == section.Expedition && record.Site == section.Site && record.Hole == section.Hole && record.Core == section.Core && record.Type == section.Type && record.Section == section.Section).FirstOrDefaultAsync().ConfigureAwait(true)); }