[英]How Would I Write This In LINQ2SQL?
I am slowly porting over an app from MySQL to use Linq2Sql - but one query has stumped me a bit. 我正在慢慢地从MySQL移植一个应用程序以使用Linq2Sql-但是一个查询使我有些困惑。
SELECT * FROM Pages WHERE DomainID = @reportid AND (PageContent REGEXP 'display:[ \\t]*none') > 0 ORDER BY URL ASC SELECT * FROM Pages DomainID = @reportid AND(PageContent REGEXP'display:[\\ t] * none')> 0按URL排序
Any ideas on how I would write something like this with Linq2SQL? 关于如何使用Linq2SQL编写类似内容的任何想法? Its the REGEXP bit thats got me stumped? 它的REGEXP有点让我难过吗?
There is no way built in to LINQ to SQL, but you have a couple of other choices. LINQ to SQL没有内置的方法,但是您还有其他两种选择。 The first is to load your strings in as in-memory objects which you can apply Regex functions to . 首先是将字符串作为内存对象加载,您可以将Regex函数应用于 。 I'm not a big fan of this since it looks like you're potentially getting some very big strings to match against. 我不太喜欢这样做,因为看起来您可能会得到一些非常大的字符串来进行匹配。
The second option is to leverage SQL CLR as described here . 第二种选择是按此处所述利用SQL CLR。 This effectively lets you create a stored procedure that gets linked to a CLR method that you create. 这有效地使您可以创建存储过程,该存储过程链接到您创建的CLR方法。 Whenever you call the method in a LINQ to SQL context, it gets converted to a stored procedure call. 每当您在LINQ to SQL上下文中调用该方法时,该方法都会转换为存储过程调用。 Then you use a query like this: 然后使用如下查询:
var q = from p in context.Pages
where p.DomainId == reportId &&
RegExMatch(p.PageContent, "display\:[ \t]*none")
select p;
Why not use LINQ to return items that match on reportid and that contain 'display:', to minimise the amount of data being returned from the server, and then use regex on client side to filter that list down? 为什么不使用LINQ返回与reportid匹配且包含'display:'的项目,以最大程度地减少从服务器返回的数据量,然后在客户端使用regex过滤该列表?
var query = Pages.Where( p => p.DomainId == 1 && p.PageContent.IndexOf("display:") > 0).OrderBy( o => o.URL );
var regex = new Regex(@"display\:[\t]*none");
foreach (var page in query)
{
if( regex.IsMatch(page.PageContent) )
{
// Do whatever...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.