Пример #1
0
        public static void CreateDatabase(CsvDataInfo[] remodeledData)
        {
            using (var db = new PostalCodesDb())
            {
                // Makes AddRange method faster.
                db.Configuration.AutoDetectChangesEnabled = false;

                db.Prefs.AddRange(CreatePrefs(remodeledData));
                Console.WriteLine("Added Prefs.");

                db.Cities.AddRange(CreateCities(remodeledData, db));
                Console.WriteLine("Added Cities.");

                db.SaveChanges();
                Console.WriteLine("Saved changes.");
            }

            using (var db = new PostalCodesDb())
            {
                // Makes AddRange method faster.
                db.Configuration.AutoDetectChangesEnabled = false;

                db.Cities.ToArray();

                db.Towns.AddRange(CreateTowns(remodeledData, db));
                Console.WriteLine("Added Towns.");

                db.SaveChanges();
                Console.WriteLine("Saved changes.");
            }
        }
Пример #2
0
 static IEnumerable <City> CreateCities(CsvDataInfo[] remodeledData, PostalCodesDb db)
 {
     return(remodeledData[1].Records
            .Select(l => new City
     {
         Code = l[1],
         Name = l[2],
         Kana = l[3],
         Pref = db.Prefs.Find(l[0]),
     }));
 }
Пример #3
0
        static IEnumerable <string> MultiTowns_Max()
        {
            using (var db = new PostalCodesDb())
            {
                var maxIndex      = db.Towns.Max(x => x.Index);
                var maxIndexTowns = db.Towns.Where(x => x.Index == maxIndex).ToArray();

                return(maxIndexTowns
                       .SelectMany(mt => db.Towns.Include("City.Pref").Where(x => x.PostalCode == mt.PostalCode))
                       .Select(x => $"{x.PostalCode.Hyphenate()} {x.Index:D2}: {x.City.Pref.Name} {x.City.Name} {x.Name}")
                       .ToArray());
            }
        }
Пример #4
0
        static IEnumerable <string> Kana_Number()
        {
            var numberPattern = new Regex("[0-9]");

            using (var db = new PostalCodesDb())
            {
                var towns = db.Towns.Include("City.Pref").ToArray();

                return(towns
                       .Where(x => numberPattern.IsMatch(x.Kana))
                       .Where(x => !numberPattern.IsMatch(x.Name))
                       .Select(x => $"{x.City.Pref.Name} {x.City.Name} {x.Name} ({x.Kana})"));
            }
        }
Пример #5
0
 static IEnumerable <Town> CreateTowns(CsvDataInfo[] remodeledData, PostalCodesDb db)
 {
     return(remodeledData[2].Records
            .GroupBy(l => l[1])
            .SelectMany(g => g
                        .Select((l, i) => new Town
     {
         PostalCode = l[1],
         Index = i,
         Name = l[2],
         Kana = l[3],
         Remarks = l[4] != "" ? l[4] : null,
         City = db.Cities.Find(l[0]),
     })));
 }
Пример #6
0
        static IEnumerable <string> TownNames_Empty()
        {
            using (var db = new PostalCodesDb())
            {
                var towns = db.Towns.Include("City.Pref")
                            .Where(x => x.Name == "")
                            .ToArray();

                return(towns
                       .GroupBy(x => x.PostalCode)
                       .Where(g => g.Count() > 1 || g.First().Remarks != "お探しの町域が見つからない場合")
                       .SelectMany(g => g)
                       .Select(x => $"{x.PostalCode.Hyphenate()} {x.Index:D2}: {x.City.Pref.Name} {x.City.Name} {x.Remarks}"));
            }
        }
Пример #7
0
        static IEnumerable <string> SingleChars()
        {
            using (var db = new PostalCodesDb())
            {
                var towns = db.Towns.Include("City.Pref").ToArray();

                var texts = towns
                            .Select(x => new { name = x.Name, text = $"{x.City.Pref.Name} {x.City.Name} {x.Name} ({x.Kana})" })
                            .GroupBy(_ => _.text)
                            .Select(g => g.First())
                            .ToArray();

                var chars = texts
                            .SelectMany(_ => _.name.Select(c => new { c, _.text }))
                            .GroupBy(_ => _.c)
                            .Where(g => g.Count() <= 1)
                            .SelectMany(g => g)
                            .ToArray();

                return(chars
                       .GroupBy(_ => _.text)
                       .Select(g => $"{string.Concat(g.Select(_ => _.c))}: {g.Key}"));
            }
        }