简体   繁体   中英

How can I get the correct values from linq query?

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

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? which is the The Organization_Reference_ID om the Included_by_Organizations xml collection. 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.

<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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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