Intersect with a custom IEqualityComparer using Linq

First of all this is wrong: public bool Equals(MyClass item1, MyClass item2) { return GetHashCode(item1) == GetHashCode(item2); } If the hashcode’s are different for sure the corresponding 2 items are different, but if they’re equal is not guaranteed that the corresponding 2 items are equal. So this is the correct Equals implementation: public bool Equals(MyClass … Read more

INTERSECT in MySQL

You can use an inner join to filter for rows that have a matching row in another table: SELECT DISTINCT records.id FROM records INNER JOIN data d1 on d1.id = records.firstname AND data.value = “john” INNER JOIN data d2 on d2.id = records.lastname AND data.value = “smith” One of many other alternatives is an in … Read more

The opposite of Intersect()

As stated, if you want to get 4 as the result, you can do like this: var nonintersect = array2.Except(array1); If you want the real non-intersection (also both 1 and 4), then this should do the trick: var nonintersect = array1.Except(array2).Union( array2.Except(array1)); This will not be the most performant solution, but for small lists it … Read more

How do I compute the intersection point of two lines?

Unlike other suggestions, this is short and doesn’t use external libraries like numpy. (Not that using other libraries is bad…it’s nice not need to, especially for such a simple problem.) def line_intersection(line1, line2): xdiff = (line1[0][0] – line1[1][0], line2[0][0] – line2[1][0]) ydiff = (line1[0][1] – line1[1][1], line2[0][1] – line2[1][1]) def det(a, b): return a[0] * … Read more

Alternative to Intersect in MySQL

Microsoft SQL Server’s INTERSECT “returns any distinct values that are returned by both the query on the left and right sides of the INTERSECT operand” This is different from a standard INNER JOIN or WHERE EXISTS query. SQL Server CREATE TABLE table_a ( id INT PRIMARY KEY, value VARCHAR(255) ); CREATE TABLE table_b ( id … Read more