Linq Lambda将表达式连接到XML

时间:2013-02-18 09:04:07

标签: linq linq-to-xml

我有一个格式如下的XML文件

<?xml version="1.0" encoding="utf-8"?>
<root>

  <EntityType_Data>
    <EntityType>
      <ID>1</ID>
      <Caption>Entity1</Caption>
      <Description>Entity1</Description>
      <ModuleID>3</ModuleID>
      <Category>1</Category>
    </EntityType>
    <EntityType>
      <ID>2</ID>
      <Caption>Entity2</Caption>
      <Description>Entity2</Description>
      <ModuleID>3</ModuleID>
      <Category>1</Category>
    </EntityType>
    <EntityType>
      <ID>3</ID>
      <Caption>Entity4</Caption>
      <Description>Entity4</Description>
      <ModuleID>3</ModuleID>
      <Category>1</Category>
    </EntityType>
    <EntityType>
      <ID>4</ID>
      <Caption>Entity5</Caption>
      <Description>Entity5</Description>
      <ModuleID>3</ModuleID>
      <Category>1</Category>
    </EntityType>
    </EntityType_Data>
  <AttributeType_Table>
    <AttributeType>
      <ID>1</ID>
      <Caption>AttributeType1</Caption>
      <DataType>string</DataType>
      <SqlType>nvarchar(max)</SqlType>
     </AttributeType>
    <AttributeType>
      <ID>1</ID>
      <Caption>AttributeType2</Caption>
      <DataType>integer</DataType>
      <SqlType>int</SqlType>
     </AttributeType>

   </AttributeType_Table>
  <Attributes_Table>
    <Attribute>
      <ID>1</ID>
      <Caption>SingleLineTextbox</Caption>
      <AttributeTypeID>1</AttributeTypeID>
      </Attribute>
    <Attribute>
      <ID>2</ID>
      <Caption>MultiLineTextBox</Caption>
      </Attribute>
    <Attribute>
      <ID>3</ID>
      <Caption>OrgLevel</Caption>
      <AttributeTypeID>3</AttributeTypeID>
      </Attribute>
  </Attributes_Table>
  <EntityRelationtable>
    <EntityTypeAttributeRelation>
      <EntityTypeID>2</EntityTypeID>
      <AttributeID>1</AttributeID>
    </EntityTypeAttributeRelation>
    <EntityTypeAttributeRelation>
      <EntityTypeID>3</EntityTypeID>
      <AttributeID>2</AttributeID>
      </EntityTypeAttributeRelation>
    <EntityTypeAttributeRelation>
      <EntityTypeID>3</EntityTypeID>
      <AttributeID>1</AttributeID>
      </EntityTypeAttributeRelation>
    <EntityTypeAttributeRelation>
      <EntityTypeID>3</EntityTypeID>
      <AttributeID>3</AttributeID>
      </EntityTypeAttributeRelation>
    <EntityTypeAttributeRelation>
      <EntityTypeID>2</EntityTypeID>
      <AttributeID>2</AttributeID>
      </EntityTypeAttributeRelation>
  </EntityRelationtable>
</root>

现在我想从“EntityRelationtable”元素中获取数据过滤器“EntityTypeID”“AttributeID”,使用where之后的where条件“AttributeID”需要从“AttributeType_Table”元素获取“标题”,“数据类型”,并且需要从“描述” >“EntityType_Data”元素。

我必须使用Linq lambda表达式执行上述要求。请仅使用带有lambda的linq解决它。

到目前为止,我尝试从“EntityRelationtable”元素中获取“EntityTypeID”“AttributeID”

var attributeresult = workingXmldoc
  .Descendants("EntitytypeAttributeRelation_Table")
  .Elements("EntityT‌​ypeAttributeRelation")
  .Where(x => x.Element("EntityTypeID").Value == "" + entitytypeId + "")
  .Select(v => new { EntitytypeID = v.Element("EntityTypeID").Value, AttributeID = v.Element("AttributeID").Value }); 

我正在考虑将此结果与“AttributeType_Table”元素“AttributeID”结合起来。

1 个答案:

答案 0 :(得分:1)

最后我用linq查询解决了我的问题,而不是linq Lambda ..

var attributeresult = from a in workingXmldoc.Descendants("EntityRelationtable").Elements("EntityTypeAttributeRelation")
                          join b in workingXmldoc.Descendants("Attributes_Table").Elements("Attribute")
                          on (string)a.Element("AttributeID") equals (string)b.Element("ID")
                          join c in workingXmldoc.Descendants("Attributetype_Table").Elements("AttributeType")
                          on (string)b.Element("AttributeTypeID") equals (string)c.Element("ID")                                                                                           
                          where (string)a.Element("EntityTypeID") == "" + entitytypeId + ""
                          select new {
                              AttributeID = a.Element("AttributeID").Value,
                              AttributeName = b.Element("Caption").Value,
                              AttributeDatatype = c.Element("DataType").Value,
                              AttributeSqltype = c.Element("SqlType").Value
                          };

如果有任何正文给Linq-lambda查询这是可以理解的..