简体   繁体   English

如何从 linq 查询中获取正确的值?

[英]How can I get the correct values from linq query?

XML XML

<wd:Report_Entry>
        <wd:Cost_Center wd:Descriptor=\"8808 ECO Global Forwarding Austria\">
            <wd:ID wd:type=\"WID\">b81295748006019ec87c29edcb020044</wd:ID>
            <wd:ID wd:type=\"Organization_Reference_ID\">8808</wd:ID>
            <wd:ID wd:type=\"Cost_Center_Reference_ID\">8808</wd:ID>
        </wd:Cost_Center>
        <wd:Type wd:Descriptor=\"Cost Center\">
            <wd:ID wd:type=\"WID\">64d1af75258301630e215c7292024500</wd:ID>
            <wd:ID wd:type=\"Organization_Type_ID\">Cost_Center</wd:ID>
        </wd:Type>
        <wd:Reference_ID>8808</wd:Reference_ID>
        <wd:code>8808</wd:code>
        <wd:name>ECO Global Forwarding Austria</wd:name>
        <wd:Included_by_Organizations wd:Descriptor=\"RU7860 Europe Forwarding Overhead\">
            <wd:ID wd:type=\"WID\">b8129574800601751d8538accb023f35</wd:ID>
            <wd:ID wd:type=\"Organization_Reference_ID\">RU7860</wd:ID>
            <wd:ID wd:type=\"Custom_Organization_Reference_ID\">RU7860</wd:ID>
        </wd:Included_by_Organizations>
        <wd:Availability_Date>1900-01-01T00:00:00.000-08:00</wd:Availability_Date>
        <wd:Is_Organization_Active>0</wd:Is_Organization_Active>
    </wd:Report_Entry>
    <wd:Report_Entry>
        <wd:Cost_Center wd:Descriptor=\"8810 Sales GF - Austria\">
            <wd:ID wd:type=\"WID\">hello</wd:ID>
            <wd:ID wd:type=\"Organization_Reference_ID\">8810</wd:ID>
            <wd:ID wd:type=\"Cost_Center_Reference_ID\">8810</wd:ID>
        </wd:Cost_Center>
        <wd:Type wd:Descriptor=\"Cost Center\">
            <wd:ID wd:type=\"WID\">6hello</wd:ID>
            <wd:ID wd:type=\"Organization_Type_ID\">Cost_Center</wd:ID>
        </wd:Type>
        <wd:Reference_ID>8810</wd:Reference_ID>
        <wd:code>8810</wd:code>
        <wd:name>Sales GF - Austria</wd:name>
        <wd:Included_by_Organizations wd:Descriptor=\"RU2302 Vienna Global Forwarding\">
            <wd:ID wd:type=\"WID\">b8129574800601968a2e289ccb02062b</wd:ID>
            <wd:ID wd:type=\"Organization_Reference_ID\">RU2302</wd:ID>
            <wd:ID wd:type=\"Custom_Organization_Reference_ID\">RU2302</wd:ID>
        </wd:Included_by_Organizations>
        <wd:Availability_Date>1900-01-01T00:00:00.000-08:00</wd:Availability_Date>
        <wd:Is_Organization_Active>0</wd:Is_Organization_Active>
    </wd:Report_Entry>   

Id Collection标识集合

var orgId = (from x in xml.Descendants(xmlNamespace + "Included_by_Organizations").Elements()
                        select x into y
                        where y.Attribute(xmlNamespace + "type").Value == "Organization_Reference_ID"
                        select y.Value);

Setting values to object properties将值设置为 object 属性

var costCenterValues = from cc in xml.Descendants(xmlNamespace + "Report_Entry")
                                   select new CostCenter
                                   {
                                       CostCenterId = cc.Element(xmlNamespace + "Reference_ID").Value,
                                       Name = cc.Element(xmlNamespace + "name").Value,
                                       Code = cc.Element(xmlNamespace + "code").Value,
                                       //CostCenterHierarchyId = 
                                   };

            return costCenterValues.ToList();

Given the xml above what is the best way to set the CostCenterHierarchyID value?鉴于上面的 xml,设置 CostCenterHierarchyID 值的最佳方法是什么? which is the The Organization_Reference_ID om the Included_by_Organizations xml collection.这是 Included_by_Organizations xml 集合的 Organization_Reference_ID。 I'm able to get all the values from the var orgId but i'm not sure how to set the correct value for each record in costCenterValues - CostCenterHierarchyId.我能够从 var orgId 获取所有值,但我不确定如何为 costCenterValues - CostCenterHierarchyId 中的每条记录设置正确的值。

<wd:Included_by_Organizations wd:Descriptor=\"RU7860 Europe Forwarding Overhead\">
            <wd:ID wd:type=\"WID\">b8129574800601751d8538accb023f35</wd:ID>
            <wd:ID wd:type=\"Organization_Reference_ID\">RU7860</wd:ID>
            <wd:ID wd:type=\"Custom_Organization_Reference_ID\">RU7860</wd:ID>
</wd:Included_by_Organizations>

Solution解决方案

.Element(xmlNamespace + "Included_by_Organizations")
.Elements(xmlNamespace + "ID")
.Where(x => x.Attribute(xmlNamespace + "type").Value == "Organization_Reference_ID")
.FirstOrDefault().Value.ToString()

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

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