{"id":20222,"date":"2022-11-09T02:16:45","date_gmt":"2022-11-08T20:46:45","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/"},"modified":"2022-11-09T02:16:45","modified_gmt":"2022-11-08T20:46:45","slug":"solved-c-optimize-logic-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/","title":{"rendered":"[Solved] C# &#8211; optimize logic [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-45775301\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"45775301\" data-parentid=\"45774665\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Your question is not well-phrased. If your <code>elem1<\/code> or <code>elem2<\/code> is a single item you want to compare against, this is pretty easy. Just do a LINQ <code>Where<\/code> to find the items that match the criteria.<\/p>\n<pre><code>Coordinates it = new Coordinates { lat = 5, lon = 5 };\nvar picked = list.Where(x =&gt; Math.Abs(x.lat - it.lat) &lt; Delta &amp;&amp; Math.Abs(x.lon - it.lon) &lt; Delta);\n<\/code><\/pre>\n<p>If, on the other hand, your two elements are any two elements in the list, then you run into a problem where you need to clarify things. As I mentioned in my comment, if your have <code>elem1<\/code>, <code>elem2<\/code>, and <code>elem3<\/code>, and if the first two don&#8217;t match the criteria but if <code>elem2<\/code> and <code>elem3<\/code> do match the criteria, then are both of those elements (2 and 3) &#8216;hits&#8217;? Assuming they are, the solution below will work. I wrote a simple Comparer to help make things easy as well.<\/p>\n<pre><code>public class CoordinateComparer : IEqualityComparer&lt;Coordinates&gt;\n{\n    public bool Equals(Coordinates x, Coordinates y)\n    {\n        return (x.lat == y.lat &amp;&amp; x.lon == y.lon);\n    }\n\n    public int GetHashCode(Coordinates obj)\n    {\n        unchecked\n        {\n            int hash = 17;\n            if (obj != null)\n            {\n                hash = hash * 23 + obj.lat.GetHashCode();\n                hash = hash * 23 + obj.lon.GetHashCode();\n            }\n            return hash;\n        }\n    }\n}\n\npublic class Coordinates\n{\n    public int lat { get; set; }\n    public int lon { get; set; }\n}\n\nclass MainClass\n{\n    public static void Main(string[] args)\n    {\n        List&lt;Coordinates&gt; list = new List&lt;Coordinates&gt;();\n        list.Add(new Coordinates { lat = 5, lon = 4 }); \n        list.Add(new Coordinates { lat = 4, lon = 5 });\n        list.Add(new Coordinates { lat = 7, lon = 4 });\n        list.Add(new Coordinates { lat = 6, lon = 3 });\n        list.Add(new Coordinates { lat = 8, lon = 2 });\n\n        double Delta = 1.1;\n\n        List&lt;Coordinates&gt; results = new List&lt;Coordinates&gt;();\n        foreach(Coordinates item in list)\n        {\n            \/\/ Find items that match the condition\n            var matches = list.Where(x =&gt; Math.Abs(x.lat - item.lat) &lt; Delta &amp;&amp; Math.Abs(x.lon - item.lon) &lt; Delta);\n            \/\/ The 'item' will always match the condition with itself, which is undesirable, so remove it\n            matches = matches.Except(new List&lt;Coordinates&gt; { item }, new CoordinateComparer());\n            \/\/ Add the items that are not already in it to the results list\n            results.AddRange(matches.Where(x =&gt; !results.Contains(x, new CoordinateComparer())));\n        }\n\n        foreach (Coordinates item in results)\n            Console.WriteLine(\"Lat : {0}, Lon : {1}\", item.lat, item.lon);\n\n        Console.ReadLine();\n    }   \n}\n<\/code><\/pre>\n<p>Bear in mind that if your latitude and longitude coordinates are either <code>double<\/code> or <code>float<\/code>, which they probably are, you might run into problems when comparing. But that&#8217;s an altogether different question.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">5<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved C# &#8211; optimize logic [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Your question is not well-phrased. If your elem1 or elem2 is a single item you want to compare against, this is pretty easy. Just do a LINQ Where to find the items that match the criteria. Coordinates it = new Coordinates { lat = 5, lon = 5 }; var picked = list.Where(x =&gt; &#8230; <a title=\"[Solved] C# &#8211; optimize logic [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/\" aria-label=\"More on [Solved] C# &#8211; optimize logic [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[352,324,578,540],"class_list":["post-20222","post","type-post","status-publish","format-standard","hentry","category-solved","tag-net","tag-c","tag-linq","tag-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] C# - optimize logic [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] C# - optimize logic [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Your question is not well-phrased. If your elem1 or elem2 is a single item you want to compare against, this is pretty easy. Just do a LINQ Where to find the items that match the criteria. Coordinates it = new Coordinates { lat = 5, lon = 5 }; var picked = list.Where(x =&gt; ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-08T20:46:45+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] C# &#8211; optimize logic [closed]\",\"datePublished\":\"2022-11-08T20:46:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/\"},\"wordCount\":160,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\".net\",\"c++\",\"linq\",\"list\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/\",\"name\":\"[Solved] C# - optimize logic [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-08T20:46:45+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] C# &#8211; optimize logic [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] C# - optimize logic [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] C# - optimize logic [closed] - JassWeb","og_description":"[ad_1] Your question is not well-phrased. If your elem1 or elem2 is a single item you want to compare against, this is pretty easy. Just do a LINQ Where to find the items that match the criteria. Coordinates it = new Coordinates { lat = 5, lon = 5 }; var picked = list.Where(x =&gt; ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/","og_site_name":"JassWeb","article_published_time":"2022-11-08T20:46:45+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] C# &#8211; optimize logic [closed]","datePublished":"2022-11-08T20:46:45+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/"},"wordCount":160,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":[".net","c++","linq","list"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/","name":"[Solved] C# - optimize logic [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-08T20:46:45+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-c-optimize-logic-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] C# &#8211; optimize logic [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/20222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=20222"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/20222\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=20222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=20222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=20222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}