T
Tom Bombadil
Question: Can an outer non-equijoin be simulated using LINQ?
It's quite unfortunate that LINQ doesn't support literal joins based on
anything other than equality. I know that this the most common scenario, and
although the efficiency of other types of joins is often debatable, they are
often darned useful. In T-SQL I have dozens of such useful queries with
joins based on such operations as prefix or suffix comparisons (using the
LIKE operator) and even bitmask comparisons.
In LINQ, one can clearly achieve the following:
(a) An outer equality-based join can be simulated using the "join" operator
in conjunction with the DefaultIfEmpty() method.
(b) One can simulate an inner join based on something other than equality,
for example:
string[] words ={"walking,"bouncing","bounced","bounce","talked","running"};
string[] suffixes = {"ing","ed","er","iest"};
....and we wish to return all words paired with their suffixes, we could use:
var pairs = from a in words from b in suffixes where a.EndsWith(b) select
new {a, b};
- but we'd only get an inner join. The word "bounce" would not be returned.
(In an outer join "bounce" would be returned paired with a null.)
Is there any way, in LINQ, of at least simulating an outer join based on
something other than equality?
It's quite unfortunate that LINQ doesn't support literal joins based on
anything other than equality. I know that this the most common scenario, and
although the efficiency of other types of joins is often debatable, they are
often darned useful. In T-SQL I have dozens of such useful queries with
joins based on such operations as prefix or suffix comparisons (using the
LIKE operator) and even bitmask comparisons.
In LINQ, one can clearly achieve the following:
(a) An outer equality-based join can be simulated using the "join" operator
in conjunction with the DefaultIfEmpty() method.
(b) One can simulate an inner join based on something other than equality,
for example:
string[] words ={"walking,"bouncing","bounced","bounce","talked","running"};
string[] suffixes = {"ing","ed","er","iest"};
....and we wish to return all words paired with their suffixes, we could use:
var pairs = from a in words from b in suffixes where a.EndsWith(b) select
new {a, b};
- but we'd only get an inner join. The word "bounce" would not be returned.
(In an outer join "bounce" would be returned paired with a null.)
Is there any way, in LINQ, of at least simulating an outer join based on
something other than equality?