- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 1668
using System; using System.Collections; using System.Linq; namespace IEnumarable { class Program { static void Main(string[] args) { MyIEnumerable myIEnumerable = new MyIEnumerable(); foreach (string test in myIEnumerable.Cast<string>().ToList()) { Console.WriteLine(test); } } class MyIEnumerable : IEnumerable { private string _names = "stanko, milosev, elizabeta, lazarevic"; public IEnumerator GetEnumerator() { string[] aryNames = _names.Split(); foreach (string name in aryNames) { yield return name; } } } } }POI:
myIEnumerable.Cast
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 1580
using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; namespace ConsoleApp15 { class Program { static void Main(string[] args) { string strXML = @" <doc> <ext>Type:A</ext> <ext>Type:B</ext> </doc>"; XElement myXML = XElement.Parse(strXML); IEnumerable<XElement> xElementData = from data in myXML.Descendants("ext") where data.Value.Contains("Type:A") select data; foreach (XElement xElement in xElementData) { Console.WriteLine(xElement.Value); } Console.ReadKey(); } } }POI:
IEnumerable
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 2350
using System; using System.Threading; namespace ThreadExample1 { class Program { static void Main(string[] args) { MyListThreadClass myListThreadClass = new MyListThreadClass(); Thread backgroundThread = new Thread(new ThreadStart(myListThreadClass.AddToMyList)); backgroundThread.Name = "Secondary"; backgroundThread.Start(); myListThreadClass.AddToMyList(); } } public class MyListThreadClass { public void AddToMyList() { for (int i = 0; i < 100; i++) { Console.WriteLine($"i: {i}, Thread.CurrentThread.ManagedThreadId: {Thread.CurrentThread.ManagedThreadId}"); } } } }First example with parameters:
using System; using System.Threading; namespace ThreadExampleWithParameters { class Program { static void Main(string[] args) { MyListThreadClass myListThreadClass = new MyListThreadClass(); Thread backgroundThread = new Thread(new ParameterizedThreadStart(myListThreadClass.AddToMyList)); backgroundThread.Name = "Secondary"; backgroundThread.Start("milosev"); myListThreadClass.AddToMyList("stanko"); } } public class MyListThreadClass { public void AddToMyList(object myName) { for (int i = 0; i < 100; i++) { Console.WriteLine($"i: {myName}{i}, Thread.CurrentThread.ManagedThreadId: {Thread.CurrentThread.ManagedThreadId}"); } } } }Here notice line: myListThreadClass.AddToMyList("stanko"); and method signature: public void AddToMyList(object myName) object Second example with parameters:
using System; using System.Threading; namespace ThreadExampleWithParameters { class Program { static void Main(string[] args) { MyListThreadClass myListThreadClass = new MyListThreadClass(); Thread backgroundThread = new Thread(delegate() { myListThreadClass.AddToMyList("milosev"); }); backgroundThread.Name = "Secondary"; backgroundThread.Start(); myListThreadClass.AddToMyList("stanko"); } } public class MyListThreadClass { public void AddToMyList(string myName) { for (int i = 0; i < 100; i++) { Console.WriteLine($"i: {myName}{i}, Thread.CurrentThread.ManagedThreadId: {Thread.CurrentThread.ManagedThreadId}"); } } } }Here notice how I define thread: Thread backgroundThread = new Thread(delegate() { myListThreadClass.AddToMyList("milosev"); }); and method signature: public void AddToMyList(string myName) string In next example, every time when something is added to list, I want to notify a method, where I will display new item added to list:
#nullable enable using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Threading; namespace ThreadExampleWithObservable { class Program { static void Main(string[] args) { MyListThreadClass<List<string>> myListThreadClass = new MyListThreadClass<List<string>>(); ObservableCollection<List<string>> myListThread = new ObservableCollection<List<string>>(); myListThread.CollectionChanged += myListThreadClass.ReadFromList; Thread backgroundWriteThread = new Thread(myListThreadClass.AddToMyList) { Name = "Secondary" }; backgroundWriteThread.Start(myListThread); myListThreadClass.AddToMyList(myListThread); } } public class MyListThreadClass<T> where T : List<string> { int _callTimes = 0; public void AddToMyList(object myListThread) { for (int i = 0; i < 100; i++) { ((ObservableCollection<List<string>>)myListThread).Add(new List<string> { $"test.AddToMyList: {i}" }); } } public void ReadFromList(object? sender, NotifyCollectionChangedEventArgs e) { _callTimes++; if (e.NewItems != null) { Console.WriteLine($"e.NewItems.Count: {e.NewItems.Count}"); foreach (List<string> myItem in e.NewItems) { Console.WriteLine($"myItem.Count: {myItem.Count}"); foreach (string itemString in myItem) { Console.WriteLine( $"itemString: {itemString}, callTimes: {_callTimes}, Thread.CurrentThread.ManagedThreadId: {Thread.CurrentThread.ManagedThreadId}"); } } } } } }Here notice line: public void AddToMyList(object myListThread) as well as that method signature is not type safe.
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 2312
In my case query looks like:
SELECT *, ( 6371 * acos ( cos (radians(41.38035200000000000000)) * cos(radians( latitude )) * cos(radians( longitude) - radians(2.16106200000000000000)) + sin (radians(41.38035200000000000000)) * sin( radians(latitude ))) ) AS distance FROM gpslocations HAVING distance < 10 ORDER BY distanceDatabase looks like this (exported with HeidiSQL):
CREATE DATABASE IF NOT EXISTS `reversegeocoding` USE `reversegeocoding`; CREATE TABLE IF NOT EXISTS `cities` ( `ID` int NOT NULL AUTO_INCREMENT, `Name` varchar(50) NOT NULL, PRIMARY KEY (`ID`,`Name`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `countries` ( `ID` int NOT NULL AUTO_INCREMENT, `Name` varchar(50) NOT NULL, PRIMARY KEY (`ID`,`Name`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `gpslocations` ( `Latitude` decimal(23,20) NOT NULL, `Longitude` decimal(23,20) NOT NULL, `FileName` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `CityID` int DEFAULT NULL, `CountryID` int DEFAULT NULL, PRIMARY KEY (`Latitude`,`Longitude`) USING BTREE, KEY `CityFK` (`CityID`), KEY `CountryFK` (`CountryID`), CONSTRAINT `CityFK` FOREIGN KEY (`CityID`) REFERENCES `cities` (`ID`), CONSTRAINT `CountryFK` FOREIGN KEY (`CountryID`) REFERENCES `countries` (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `gpslocationsgroupedby10kmdistancs` ( `ID` int NOT NULL AUTO_INCREMENT, `Latitude` decimal(23,20) NOT NULL, `Longitude` decimal(23,20) NOT NULL, `FileName` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, `CityID` int DEFAULT NULL, `CountryID` int DEFAULT NULL, PRIMARY KEY (`ID`,`Latitude`,`Longitude`) USING BTREE, KEY `City10KmFK` (`CityID`), KEY `Country10KmFK` (`CountryID`), CONSTRAINT `City10KmFK` FOREIGN KEY (`CityID`) REFERENCES `cities` (`ID`), CONSTRAINT `Country10KmFK` FOREIGN KEY (`CountryID`) REFERENCES `countries` (`ID`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;Here is my Code:
using System; using System.Collections.Generic; using System.Linq; using MySql.Data.MySqlClient; namespace Radius { class Program { private static string ConnectionString => "SERVER=localhost;DATABASE=reversegeocoding;UID=root;PASSWORD=myPass;"; public class LatLngFileNameModel { public string Latitude { get; set; } public string Longitude { get; set; } public string FileName { get; set; } public string CityID { get; set; } public string CountryID { get; set; } } static void Main(string[] args) { List<LatLngFileNameModel> latLngFileNames = new List<LatLngFileNameModel>(); string sqlGpslocations = "select * from gpslocations"; using MySqlConnection mySqlConnection = new MySqlConnection(ConnectionString); using MySqlCommand mySqlCommand = new MySqlCommand(sqlGpslocations, mySqlConnection); mySqlConnection.Open(); MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader(); while (mySqlDataReader.Read()) { LatLngFileNameModel latLngFileName = new LatLngFileNameModel { Latitude = mySqlDataReader["Latitude"].ToString().Replace(',', '.') , Longitude = mySqlDataReader["Longitude"].ToString().Replace(',', '.') , FileName = mySqlDataReader["FileName"].ToString() , CityID = mySqlDataReader["CityID"].ToString() , CountryID = mySqlDataReader["CountryID"].ToString() }; Console.WriteLine($"Geocoding: {latLngFileName.Latitude}, {latLngFileName.Longitude}, FileName: {latLngFileName.FileName}"); if (!IsWithinRadius(latLngFileName.Latitude, latLngFileName.Longitude, latLngFileNames, "10")) { using MySqlCommand gpslocationsgroupedby10kmdistancs = new MySqlCommand(); using MySqlConnection mySqlConnectionGpslocationsgroupedby10kmdistancs = new MySqlConnection(ConnectionString); mySqlConnectionGpslocationsgroupedby10kmdistancs.Open(); gpslocationsgroupedby10kmdistancs.Connection = mySqlConnectionGpslocationsgroupedby10kmdistancs; gpslocationsgroupedby10kmdistancs.CommandText = $"INSERT INTO reversegeocoding.gpslocationsgroupedby10kmdistancs (Latitude, Longitude, FileName, CityID, CountryID) VALUES " + $"('{latLngFileName.Latitude}'" + $", '{latLngFileName.Longitude}'" + $", '{latLngFileName.FileName}'" + $", '{latLngFileName.CityID}'" + $", '{latLngFileName.CountryID}'" + ");"; gpslocationsgroupedby10kmdistancs.ExecuteNonQuery(); latLngFileNames.Add(latLngFileName); } } } private static bool IsWithinRadius(string latitude, string longitude, List<LatLngFileNameModel> latLngFileNames, string distanceKm = "0.5") { if (latLngFileNames.Any()) { latitude = latitude.Replace(',', '.'); longitude = longitude.Replace(',', '.'); string sqlRadius = "SELECT *, " + "(6371 * acos (" + $"cos ( radians({latitude})) " + "* cos( radians( latitude )) " + "* cos( radians( longitude) " + $"- radians({longitude})) " + $"+ sin (radians({latitude})) " + "* sin( radians( latitude )) " + ")) AS distance " + "FROM gpslocations " + $"HAVING distance < {distanceKm} " + "ORDER BY distance "; using (MySqlConnection mySqlConnection = new MySqlConnection(ConnectionString)) using (MySqlCommand mySqlCommand = new MySqlCommand(sqlRadius, mySqlConnection)) { mySqlConnection.Open(); MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader(); while (mySqlDataReader.Read()) { LatLngFileNameModel latLngFileName = new LatLngFileNameModel { Latitude = mySqlDataReader["Latitude"].ToString().Replace(',', '.'), Longitude = mySqlDataReader["Longitude"].ToString().Replace(',', '.'), FileName = mySqlDataReader["FileName"].ToString() }; if (latLngFileNames.Any(latLng => latLng.Latitude == latLngFileName.Latitude && latLng.Longitude == latLngFileName.Longitude)) { return true; } } } } return false; } } }Example project download from here