Linq 2 SQL语法帮助

时间:2010-07-22 12:34:38

标签: sql sql-server linq linq-to-sql

我几乎已经完成了将MySQL应用程序转换为Linq2SQL,但很难在最后一次SQL查询中找到答案

SELECT a.URL, b.URL AS DuplicateURL
FROM Pages a
INNER JOIN Pages b ON a.MetaDescription = b.MetaDescription
                   AND a.PageID <> b.PageID
WHERE a.DomainID = @reportid
  AND b.DomainID = @reportid
  AND LENGTH(TRIM(a.MetaDescription)) > 0
  AND a.URL < b.URL
ORDER BY a.URL ASC

我能不能给我一些关于如何在Linq2SQL中创建这个查询的语法帮助?

任何帮助非常感谢

2 个答案:

答案 0 :(得分:2)

这很简单:

var reportId = ...;

var duplicates =
    from a in db.Pages
    from b in db.Pages
    where a.MetaDescription == b.MetaDescription
    where a.PageID != b.PageID
    where a.DomainID == reportId
    where b.DomainID == reportId
    where a.MetaDescription.Trim().Length > 0
    where a.URL < b.URL
    orderby a.URL
    select new { Url = a.URL, DuplicateUrl = b.Url }

答案 1 :(得分:2)

它是这样的:

var DuplicatePages = 
    from a in DBContext.Pages
    join b in DBContext.Pages on a.MetaDescription equals b.MetaDescription
    where (a.PageID <> b.PageID) && (a.DomainID == ReportID) && 
    (b.DomainID == ReportID) && (a.MetaDescription.Trim().Length > 0) && 
    (a.URL < b.URL)
    orderby a.URL
    select new { Url = a.URL, DuplicateUrl = b.URL };
相关问题