简体   繁体   English

Linq比较XML值

[英]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. 我不敢相信使用linq比较两个XML列以查看它们是否相等是如此困难。 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. 我尝试了直接比较,并尝试使用.ToString()方法进行比较。 Both the attempts I made threw a useless error... 我的两次尝试都引发了无用的错误...

My original linq statement: 我最初的linq声明:

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# ". 更新 :我也想到了“ 拧紧它,我将带回XML并在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. 我讨厌不得不将XML从我的数据服务器中带回来,只是看它们是否相等,但是至少我现在可以比较这些值。

Is there a way to just determine if the two XML columns are equal using linq? 有没有一种方法可以使用linq来确定两个XML列是否相等?

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 解决方案1:我使用Linq SQL,也许您可​​以创建一个SQL Server计算列,并通过示例将您的XML列转换为varchar(2000),然后将此列用于linq

Solution 2 : Create a view with convert column into varchar and use into Linq 解决方案2:创建一个视图,并将convert列转换为varchar并用于Linq

Solution 3 : Create a view with your complete query and linq into this view 解决方案3:使用完整的查询创建视图,并在该视图中使用linq

Solution 4 (i dont kwo if its work) : 解决方案4(如果工作,我不知道):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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