简体   繁体   中英

Using SQL “CAST” within LINQ

I'm trying to get XMl from my SQL database, using CAST withing the query.

This is how I tried to make it work:

string str = db.Database.SqlQuery<string>("SELECT CAST(CAST(Content AS VARBINARY(MAX)) AS XML) AS DashboardXML FROM ReportServer$SRVSQL2012.dbo.Catalog WHERE Name = 'DannyTest.xml'").FirstOrDefault<string>();

But this doesn't really work as all < and > get's replaced by &lt and &gt .

How does one accomplish such task with pure LINQ? Can it even be done?

EDIT

I'm not even sure if this is a service problem or what it is that actually replaces the < and > ?

EXAMPLE OF OUTCOME

&lt;Dashboard CurrencyCulture="da-DK"&gt

should be

<Dashboard CurrencyCulture="da-DK">

SOLVED

Instead of using the XElement.load("whatever") I used the XElement.Parse("whatever") since the output from the LINQ statement is a string.

Like so:

XElement xelement = XElement.Parse(
    db.Database.SqlQuery<string>(
        "SELECT CAST(CAST(Content AS VARBINARY(MAX)) AS XML) 
        AS DashboardXML FROM ReportServer$SRVSQL2012.dbo.Catalog WHERE Name = 'DannyTest.xml'")
    .FirstOrDefault<string>());

This way, the string got converting correctly into a readable XML

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM