开发者

LINQ Equivalent of SQL

开发者 https://www.devze.com 2023-01-09 06:49 出处:网络
I have to translate the following SQL Query into LINQ equivalent S开发者_运维知识库ELECT 0 AS DOCID,

I have to translate the following SQL Query into LINQ equivalent

S开发者_运维知识库ELECT 
    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

How to translate the above into LINQ equivalents?


Assuming you've taken care of the union in your database and retrieve your data through a view, it could be this:

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


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 });

Note: completely untested code. No guarantees.


Tested this with a hastily-made table based on your SQL. I hope it's what you're looking for:

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
             });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号