177-linq-join-operator-in-c

In this article, we have LINQ example of usage of join operator in C#. Perhaps, the easiest way to explain usage of join operator in C# is by example. In first example we have two arrays of double type. Now, we want to select those members of first array that have odd counterparts in another array:

double[] set1 = { 2, 3, 0, 10, -10 };
double[] set2 = { 5, 7, 2, 10 };

var result = from t in set1

join x in set2 on (2*t + 1) equals x

select t;

Another example is to select those members of one array that have absolute value counterparts in another array.

double[] set1 = { 2, 3, 0, 10, -10 };
double[] set2 = { 5, 7, 2, 10 };

var result = from t in set1

join x in set2 on Math.Abs(t) equals x

select t;

Take a note that for “join” LINQ operator in C#, operator for checking equality is keyword “equals”, not symbol “=”.

External links:

LINQ Join Operator in C# on Microsoft
LINQ Join Operator in C# on Dotnetperls

Posted in c-sharp-code-examples

Leave a Reply

Your email address will not be published. Required fields are marked *

*