Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.IO;
- // new
- namespace rdc.KLADR
- {
- public class KLADR
- {
- public string city { get; set; }
- public string status { get; set; }
- internal static void removeCity(string city)
- {
- RDCEntities context = new RDCEntities();
- foreach (KLADRMain rowToDelete in (from r in context.KLADRMains where r.City == city select r))
- {
- context.DeleteObject(rowToDelete);
- }
- context.SaveChanges();
- }
- public string importCityFromFile(string pathToFile)
- {
- string line;
- StreamReader cityFile = new StreamReader(pathToFile, System.Text.Encoding.GetEncoding(866));
- int countLines = 0;
- while ((line = cityFile.ReadLine()) != null)
- {
- countLines++;
- if (countLines == 2)
- {
- line = line.Trim();
- city = line.Substring(0, line.IndexOf(" - УЛИЦЫ"));
- }
- }
- status = "Город успешно вычленен из файла";
- cityFile.Close();
- return city;
- }
- public bool cityAlreadyInDB()
- {
- RDCEntities context = new RDCEntities();
- int rowsWithCity = context.KLADRMains.Where(r => r.City == city).Count();
- status = rowsWithCity.ToString() + " улиц по городу " + city + " уже есть.";
- if (rowsWithCity == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- public void addCityFromFileToDB(string pathToFile)
- {
- string line;
- RDCEntities eKLADR = new RDCEntities();
- StreamReader cityFile = new StreamReader(pathToFile, System.Text.Encoding.GetEncoding(866));
- int countLines = 0;
- while ((line = cityFile.ReadLine()) != null)
- {
- countLines++;
- string fullType = "";
- string shortType = "";
- if (countLines > 5 && !line.StartsWith("Всего отобрано"))
- {
- string[] eachStreet = line.Split(';');
- shortType = eachStreet[1].Trim();
- fullType = (from r in eKLADR.KLADRMains where r.City == "ACR_NO_CITY" && r.StreetName == shortType select r).First().StreetType;
- // rdc.utils.indexToFirst();
- eKLADR.AddToKLADRMains(KLADRMain.CreateKLADRMain(city, Utils.indexToFirst(eachStreet[0].Trim()), fullType, 0));
- eKLADR.SaveChanges();
- }
- }
- status = "Записи загружены";
- cityFile.Close();
- }
- internal static void addCityFromCSVToDB(string pathToFile)
- {
- string line;
- RDCEntities eKLADR = new RDCEntities();
- StreamReader cityFile = new StreamReader(pathToFile, System.Text.Encoding.GetEncoding(1251));
- // int countLines = 0;
- while ((line = cityFile.ReadLine()) != null)
- {
- string[] eachStreet = line.Split(';');
- eKLADR.AddToKLADRMains(KLADRMain.CreateKLADRMain(eachStreet[0], eachStreet[1], eachStreet[2], 0));
- eKLADR.SaveChanges();
- }
- cityFile.Close();
- }
- internal static void fullFill(ref List<string[]> table, int streetNameCol, int streetTypeCol, string cityName)
- {
- RDCEntities context = new RDCEntities();
- string streetType;
- string streetName;
- foreach (string[] line in table)
- {
- streetType = line[streetTypeCol].Trim();
- streetName = Utils.indexToFirst(line[streetNameCol].Trim());
- line[streetNameCol] = streetName;
- if (string.IsNullOrWhiteSpace(streetType) && !string.IsNullOrWhiteSpace(streetName))
- {
- // try
- //{
- if (context.KLADRMains.Where(p => p.City == cityName).Where(p => p.StreetName == streetName).Count() == 1)
- {
- line[streetTypeCol] = (from p in context.KLADRMains where p.City == cityName && p.StreetName == streetName select p).FirstOrDefault().StreetType;
- }
- //}
- //catch (Exception e)
- //{ }
- }
- }
- }
- internal static List<string> showAllCities()
- {
- RDCEntities context = new RDCEntities();
- List<string> cityList;
- cityList = (from p in context.KLADRMains where p.City != "ACR_NO_CITY" select p.City).Distinct().ToList();
- cityList.Sort();
- return cityList;
- }
- internal static void RenameCity(string city, string newCity)
- {
- RDCEntities context = new RDCEntities();
- foreach (KLADRMain obj in (from r in context.KLADRMains where r.City == city select r))
- {
- obj.City = newCity;
- }
- context.SaveChanges();
- }
- //internal static List<string> ShowStreetsInCity(string city)
- //{
- // RDCEntities context = new RDCEntities();
- //// return (from r in context.KLADRMains where r.City == city select new { r.StreetName, r.StreetType }).ToList();
- //}
- internal static void fullFilldoubleOpt1(ref List<string[]> table, int streetNameCol, int streetTypeCol, string cityName)
- {
- RDCEntities context = new RDCEntities();
- string streetType;
- string streetName;
- foreach (string[] line in table)
- {
- streetType = line[streetTypeCol].Trim();
- streetName = Utils.indexToFirst(line[streetNameCol].Trim());
- line[streetNameCol] = streetName;
- int tempTryParse;
- if (string.IsNullOrWhiteSpace(streetType) && !string.IsNullOrWhiteSpace(streetName))
- {
- // try
- //{
- if (context.KLADRMains.Where(p => p.City == cityName).Where(p => p.StreetName == streetName).Count() == 1)
- {
- line[streetTypeCol] = (from p in context.KLADRMains where p.City == cityName && p.StreetName == streetName select p).FirstOrDefault().StreetType;
- }
- else
- {
- if (!int.TryParse(streetName.Substring(0, 1), out tempTryParse) && streetName.Split().Length == 2)
- {
- string partOfStreetName = streetName.Split()[1];
- var possibleResults = (from p in context.KLADRMains where p.City == cityName && p.StreetName.Contains(partOfStreetName) select p);
- foreach (var possibleResult in possibleResults)
- {
- if (possibleResult.StreetName.Split(' ').Length == 2 && possibleResult.StreetName.Split(' ')[1] == partOfStreetName)
- {
- line[streetNameCol] = possibleResult.StreetName;
- line[streetTypeCol] = possibleResult.StreetType;
- }
- }
- }
- //if (context.KLADRMains.Where(p => p.City == cityName).Where(p => p.StreetName.Contains(streetName.Substring(2).Trim())).Count() == 1) ;
- //{
- //}
- //}
- //catch (Exception e)
- //{ }
- }
- }
- }
- }
- internal static void fullFilldoubleOpt1E(ref List<string[]> table, int streetNameCol, int streetTypeCol, string cityName)
- {
- RDCEntities context = new RDCEntities();
- string streetType;
- string streetName;
- foreach (string[] line in table)
- {
- streetType = line[streetTypeCol].Trim();
- streetName = Utils.indexToFirst(line[streetNameCol].Trim());
- line[streetNameCol] = streetName;
- int tempTryParse;
- if (string.IsNullOrWhiteSpace(streetType) && !string.IsNullOrWhiteSpace(streetName))
- {
- // try
- //{
- if (context.KLADRMains.Where(p => p.City == cityName).Where(p => p.StreetName == streetName).Count() == 1)
- {
- line[streetTypeCol] = (from p in context.KLADRMains where p.City == cityName && p.StreetName == streetName select p).FirstOrDefault().StreetType;
- }
- else
- {
- if (!int.TryParse(streetName.Substring(0, 1), out tempTryParse) && streetName.Split().Length == 2) // если двойное название
- {
- string partOfStreetName = streetName.Split()[1];
- var possibleResults = (from p in context.KLADRMains where p.City == cityName && p.StreetName.Contains(partOfStreetName) select p);
- foreach (var possibleResult in possibleResults)
- {
- if (possibleResult.StreetName.Split(' ').Length == 2 && possibleResult.StreetName.Split(' ')[1] == partOfStreetName)
- {
- line[streetNameCol] = possibleResult.StreetName;
- line[streetTypeCol] = possibleResult.StreetType;
- }
- }
- }
- if (!int.TryParse(streetName.Substring(0, 1), out tempTryParse) && streetName.Split().Length == 1) // если название из одного слова
- {
- var possibleResults = (from p in context.KLADRMains where p.City == cityName && p.StreetName.Contains(streetName) select p);
- List<string[]> possibleStreets = new List<string[]>();
- foreach (var possibleResult in possibleResults)
- {
- if (possibleResult.StreetName.Split(' ').Length == 2 && possibleResult.StreetName.Split(' ')[1] == streetName)
- {
- //line[streetNameCol] = possibleResult.StreetName;
- //line[streetTypeCol] = possibleResult.StreetType;
- possibleStreets.Add(new string[] { possibleResult.StreetName, possibleResult.StreetType });
- }
- }
- if (possibleStreets.Count == 1)
- {
- line[streetNameCol] = possibleStreets[0][0];
- line[streetTypeCol] = possibleStreets[0][1];
- }
- }
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment