使用Linq搜索表XML可以完成吗? [英] Searching a tables XML filed using Linq, can it be done?

查看:51
本文介绍了使用Linq搜索表XML可以完成吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含交易记录的数据库表.在其中一个字段中,有一条XML消息,字段类型为"xml".在此XML中,有一个员工被提起,我需要搜索的是此字段.我想检索与运行时提供的员工编号匹配的所有行.有可能用Linq做到这一点?

I have a database table with transactions. In one of the fields there is an XML message, the field type is "xml". In this XML there is an employee filed and it is this field that I need to search. I want to retrieve all the rows that match the employee number supplied at runtime. Is it possible to do this with Linq ?

此处显示的是交易表中某行中的一些示例XML.该字段称为消息".我需要查看员工"值,如果该行与用户提供的内容匹配,则返回该行.

Shown here is some example XML from one of the rows in the transaction table. The field is called "Message". I need to look at the "employee" value and return the row if it match what was supplied by the user.

<interface>
  <mac>1452345234</mac>
  <device>device1</device>
  <id>1234567</id>
  <terminal>
    <unit>1</unit>
    <trans>
      <event>A3</event>
      <employee>3333</employee>
      <time>2008-10-02T11:41:00.0000000+00:00</time>
    </trans>
  </terminal>
</interface>

推荐答案

是的,使用LINQ很容易实现:

Yes, it is easily possible with LINQ:

var matchList = from t in transactions
                where XDocument.Load (new StringReader (t.Message))
                               .Descendants ("employee")
                               .Count (node => node.Value == employeeNr) > 0
                select t;

这篇关于使用Linq搜索表XML可以完成吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆