views:

40

answers:

2

I'm actually using a join in linqtosql (via dblinq).

I'm trying to include a regular expression in the join part of the linq query.

from i in collectiona
join j in collectionb on Regex.IsMatch(i.name, j.jokered_name) equals true
(...)

I agree i can push the RegExp check in the where part of the linq query, but i was wondering if it is possible in the join part ? The above code wants an "i equals j" code structure.

One thing i think to perform is overriding Equals() which 'll contains the RegEx.IsMatch() stuff and put a simple i equals j in the join part.

Any suggestions about my problem ?

+3  A: 

It's inappropriate in the join clause because the joins in LINQ are equijoins - they're checking whether some projection from one sequence is equal to a projected value in the other sequence. That's not what you're trying to do here - you're just testing a condition which depends on both values together.

A where clause is more suitable here:

from i in collectiona
from j in collectionb
where Regex.IsMatch(i.name, j.jokered_name)
select ...

However, I've only just seen that this is LINQ to SQL - I don't know whether you can use regular expressions at all in LINQ to SQL.

Jon Skeet
You can use a user-defined function to do it through the L2S model...but you're correct there's no native support for anything like this.
Nick Craver
Thank you. I actually use the where condition successfully, but i was trying to dig the join and especially the equals keyword. You answer lights me perfectly.
Graveen
+1  A: 
from i in collectiona 
join j in collectionb
on x equals y

This code is translated into a call to Enumerable.Join:

collectiona.Join(
  collectionb,
  i => x,
  j => y,
  (i, j) => new {i, j}
)

From here, it is easy to see that i is in scope for the x expression, while j is not.


from i in collectiona
from j in collectionb
where z

is equivalent to:

collectiona
  .SelectMany(i => collectionb, (i, j) => new {i, j})
  .Where(q => z)

Each q has an i and a j available for use in z expression.

David B