private static void ExportDrugActivities(DossierContext context) { var activities = context.Individuals .Where(i => i.Activities .Any(a => a.ActivityType.Name == "DrugTrafficking")) .OrderBy(i => i.FullName) .Select(i => new { fullName = i.FullName, birthDate = i.BirthDate, activities = i.Activities .Where(a => a.ActivityType.Name == "DrugTrafficking") .OrderBy(a => a.ActiveFrom) .Select(a => new { description = a.Description, activeFrom = a.ActiveFrom, activeTo = a.ActiveTo }) }); var json = JsonConvert.SerializeObject(activities, Formatting.Indented); File.WriteAllText("drug-activities.json", json); }
static void Main() { var context = new DossierContext(); ExportActiveIndividuals(context); ExportDrugActivities(context); ExportOsloVisitors(context); UpdateMissingIndividuals(context); }
private static void UpdateMissingIndividuals(DossierContext context) { var updatedRows = context.Individuals .Where(i => !i.Locations .Any(l => l.LastSeen.Year >= 2010) && i.Status == Status.Active) .Update(i => new Individual { Status = Status.Missing }); Console.WriteLine(updatedRows); }
private static void ImportActivityTypes() { var context = new DossierContext(); var json = File.ReadAllText("../../data/activity-types.json"); var activityTypes = JsonConvert.DeserializeObject <IEnumerable <ActivityTypeDTO> >(json); foreach (var type in activityTypes) { context.ActivityTypes.Add(new ActivityType() { Name = type.Name }); } context.SaveChanges(); }
private static void ExportActiveIndividuals(DossierContext context) { var activeIndividuals = context.Individuals .Where(i => i.Status == Status.Missing) .OrderBy(i => i.FullName) .Select(i => new { id = i.Id, fullName = i.FullName, alias = i.Alias }) .ToList(); var json = JsonConvert.SerializeObject(activeIndividuals, Formatting.Indented); File.WriteAllText("active-individuals.json", json); }
private static void InitializeDictionaries() { var context = new DossierContext(); activityTypeIds = new Dictionary <string, int>(); citiesIds = new Dictionary <string, int>(); context.ActivityTypes .Select(at => new { at.Id, at.Name }) .ToList() .ForEach(el => activityTypeIds[el.Name] = el.Id); context.Cities .Select(c => new { c.Id, c.Name }) .ToList() .ForEach(c => citiesIds[c.Name] = c.Id); }
private static void ImportCities() { var context = new DossierContext(); var xml = XDocument.Load("../../data/cities.xml"); var cities = xml.XPathSelectElements("cities/city"); foreach (var city in cities) { var cityName = city.Attribute("name").Value; context.Cities.Add(new City() { Name = cityName }); } context.SaveChanges(); }
private static void ExportOsloVisitors(DossierContext context) { var data = context.Individuals .Where(i => i.Locations .Any(l => l.City.Name == "Jurish" && l.LastSeen.Year < 2005)) .Select(i => new { fullName = i.FullName, locations = i.Locations .OrderBy(l => l.LastSeen) .Select(l => new { seen = l.LastSeen, }) }); var json = JsonConvert.SerializeObject(data, Formatting.Indented); File.WriteAllText("oslo-visitors.json", json); }
private static void ImportIndividuals() { var context = new DossierContext(); var json = File.ReadAllText("../../data/individuals.json"); var individuals = JsonConvert.DeserializeObject <IEnumerable <IndividualDTO> >( json, new IsoDateTimeConverter { DateTimeFormat = "dd/MM/yyyy" }); foreach (var individual in individuals) { var exists = context.Individuals.Any(i => i.Id == individual.Id); if (exists) { Console.WriteLine("{0} is already present in the database.", individual.FullName); continue; } var individualEntity = new Individual { Id = individual.Id, FullName = individual.FullName, Alias = individual.Alias, Status = individual.Status, }; if (individual.BirthDate.HasValue) { individualEntity.BirthDate = individual.BirthDate.Value; } foreach (var activityDto in individual.Activities) { var activity = new Activity { Description = activityDto.Description, ActiveFrom = activityDto.ActiveFrom, ActivityTypeId = activityTypeIds[activityDto.ActivityType], IndividualId = individualEntity.Id }; if (activityDto.ActiveTo.HasValue) { activity.ActiveTo = activityDto.ActiveTo.Value; } individualEntity.Activities.Add(activity); } foreach (var locationDto in individual.Locations) { var location = new Location { CityId = citiesIds[locationDto.City], LastSeen = locationDto.LastSeen, IndividualId = individualEntity.Id }; individualEntity.Locations.Add(location); } context.Individuals.Add(individualEntity); context.SaveChanges(); Console.WriteLine("Successfully imported data for {0}", individual.FullName); } }