简体   繁体   中英

Linq compare XML Values

I cannot believe it is so difficult to just compare two XML columns using linq to see if they are equal. This only happens in my system once in a blue moon, but I want to account for it when it does.

I have tried a straight comparison, and attempted the comparison with the .ToString() method. Both the attempts I made threw a useless error...

My original linq statement:

from al in ActivityLogs
join a in Activities on al.ActivityId equals a.ActivityId
join dva in AllDVAudits on al.ActivityLogId equals dva.ActivityLogId
where al.ProfileId == ProfileID 

I have tried adding this:

Where al.ProfileId == ProfileID 
  && al.OldData != al.NewData

And this

Where al.ProfileId == ProfileID    
 && al.OldData.ToString() != al.NewData.ToString()

UPDATE : I also thought to myself " Screw it, I will just bring back the XML and manipulate it ( compare it ) in C# ". I hate to have to bring the XML back from my data server just to see if they are equal, but at least I can compare the values at this point.

Is there a way to just determine if the two XML columns are equal using linq?

Any insight greatly appreciated.

Solution 1 : I you do Linq SQL, may be you can create a computed column is SQL Server with cast your XML column into varchar(2000) by example and use this column into your linq

Solution 2 : Create a view with convert column into varchar and use into Linq

Solution 3 : Create a view with your complete query and linq into this view

Solution 4 (i dont kwo if its work) :

    var all = from bm in context.MYTABLEs
              select new { name = bm.SPP_USER_ID, xml = (string)bm.SPP_BOOKMARKS_XML };

    var docs = from x in all
               select XDocument.Parse(x.xml);

    var href = from h in docs
               select h.Descendants("href");

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