Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Suppose that I have a table such as

"t.[vf]$" | "Russian"
"tt[io]$" | "Italian"
"[dl]ez$" | "Spanish"

so that, given Potov or Potef I'll get Russian, etc. Evidently EndsWith doesn't cut the mustard, and

var q = from r in db.Origins where Regex.IsMatch("Potov", r.Pattern) select r.Origin;
gets me

"Method 'Boolean IsMatch(System.String, System.String)' has no supported translation to SQL."

Fair enough. Is there any way to do this?

Thanks
Posted

You'll have to force the Origins collection into an in-memory Enumerable, first:

var q = from r in db.Origins.AsEnumerable() where Regex.IsMatch("Potov", r.Pattern) select r.Origin;


Now Linq is not looking for IsMatch() as a SQL function, but as a .Net object's method - which it will find.

See Enumerable.AsEnumerable<TSource>[^].

Linq-to-SQL objects implement IQueryable<TSource>[^] - which translates the Lambdas into SQL queries (which, in the case of your example above, fails). The cast to AsEnumerable forces Linq to use the Linq-to-objects Where method instead.
 
Share this answer
 
v2
Comments
Sandeep Mewara 30-Apr-11 2:01am    
My 5!
You'll have to either do the regular expression comparison in a Linq-to-Objects query rather than Linq-to-SQL, or call a stored procedure that will make use of a CLR user-defined function to apply the regular expression.

See this MSDN article for an example of using the CLR in SQL Server to use regular expressions: http://msdn.microsoft.com/en-us/magazine/cc163473.aspx[^]
 
Share this answer
 
Do you mean to derive nationality or ethnicity from the form of the name using some set of rules?
This is what I can see from this question. Is is something like that?

Please correct me if I'm wrong, but it this is the case, this is not just impossible or makes no sense — this is totally wrong. This is not about programming but about simple logic. I would not even explain why it's wrong — this would be really beyond good and evil…

—SA
 
Share this answer
 
Comments
James H 19-Jul-21 12:26pm    
Hey Sergey - I can tell from your name you're Russian ("t.[vf]$" | "Russian") :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900