2009-12-02 27 views
1

我有一个包含事务的数据库表。在其中一个字段中有一条XML消息,字段类型为“xml”。在这个XML中有一个雇员提交,我需要搜索这个字段。我想检索与运行时提供的员工编号相匹配的所有行。 Linq可以做到这一点吗?使用Linq搜索表格XML文件,可以完成吗?

这里展示的是从交易表中的行之一的一些示例XML。该字段被称为“消息”。我需要查看“员工”值,并返回与用户提供的内容相匹配的行。

<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> 
+0

你要过滤数据库或应用程序的结果? – 2009-12-02 12:56:52

+1

请参阅http://stackoverflow.com/questions/282391/can-linq-to-sql-query-an-xml-field-db-serverside – 2009-12-02 13:05:52

+0

我应该提到我正在使用实体并希望在应用程序中进行筛选。 – Retrocoder 2009-12-02 15:15:45

回答

1

是的,这是很容易可能的LINQ:

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

简单的方法:

List<YourRecord> GetRecords(string EmployeeId) 
{ 
    return TheTable.where(r => r.Message.Contains("<employee>" + empId + "</employee>")).ToList(); 
} 
+0

不幸的是你的解决方案给出了以下错误:“不允许将数据类型xml隐式转换为数据类型nvarchar,表'transactions','Message'列。使用CONVERT函数来运行这个查询。“任何想法我应该如何使用”CONVERT“函数? – Retrocoder 2009-12-02 14:28:34

+0

我应该提到我正在使用实体 – Retrocoder 2009-12-02 14:38:40

相关问题