LINQ等效于SQL

时间:2010-07-27 17:45:40

标签: c# linq linq-to-sql

我必须将以下SQL Query转换为LINQ等效项

SELECT 
    0 AS DOCID, 
    'All_Forms ' as PAGE, 
    0 AS PAGENUMBER

UNION 

SELECT 
    DOCID,
    (CAST(IsNull(CUSTOMPAGE,PAGENUMBER) AS VARCHAR(10)) +'. '+TITLE ) AS PAGE,
    PAGENUMBER FROM Medical_Reports 
WHERE
    PAPERSTYLE='Normal' 
    AND PAGENUMBER<>10000 
ORDER BY
    docid

如何将上述内容翻译成LINQ等价物?

3 个答案:

答案 0 :(得分:1)

假设您已经处理了数据库中的联合并通过视图检索数据,可能是这样的:

from reports in medicalReports
where reports.PaperStyle == "Normal"
&& reports.PageNumber != 10000
order by reports.DocId
select reports

答案 1 :(得分:0)

var reps = from r in Medical_Reports
               where r.PaperStyle  == 'Normal' && r.PageNumber != 10000
               order by r.DocId
           select { DocId = r.DocId,
                    Page = ((string)(r.CustomPage == r.PageNumber) + ". " + r.Title,
                    PageNumber = r.PageNumber };

reps.Add({ DocId = 0, Page = "All_Forms ", PageNumber = 0 });

注意:完全未经测试的代码。没有保证。

答案 2 :(得分:0)

使用基于SQL的仓促表格对此进行测试。我希望这就是你要找的东西:

var firstItem = new List<dynamic>() { 
    new { DocId = 0, Page = "All_Forms ", PageNumber = 0}
};

var pages = (from p1 in firstItem
             select p1).Union(
             from p2 in MedicalReports
             where p2.PaperStyle == "Normal" &&
                    p2.PageNumber != 10000
             orderby p2.DocId
             select new
             {
                 DocId = p2.DocId,
                 Page = ((p2.CustomPage != null) ? p2.CustomPage : p2.PageNumber.ToString()) + ". " + p2.Title,
                 PageNumber = p2.PageNumber
             });