Maverick1415
Maverick1415

Reputation: 113

Adding results to a dictionary using xelement

private Dictionary<int, double> TaxDiction { set; get; }     

XDocument doc = XDocument.Load(path);
XElement xelement = XElement.Load(path);
var query = from nm in xelement.Descendants("EmployeeFinance")
            where (int)nm.Element("EmpPersonal_Id") == empID
            select new AllowancePaid
            {
                TaxDiction = ??
            };
var resultquery = query.SingleOrDefault();

Edited I am selecting from an xml file and would like to insert the values into a dictionary (TaxDiction). before I would select the two values and insert it into separate list as shown below:

list1 = nm.Element("ListOfTaxableAllowance").Elements("Amount").Attributes("BenListId").Select(a => (int)a).ToList(),
list2 = nm.Element("ListOfTaxableAllowance").Elements("Amount").Select(a => (double)a).ToList()

However the values from the two list are related and i would like to insert them into a dictionary. so the values in list1 will be the keys and the values from list2 will my the values in my dictionary. I thought it would be more efficient like this as the values are related. I hope this help to clear things up a little. thank you.

EDITED

I tried....

TaxDiction.Add(nm.Element("ListOfTaxableAllowance").Elements("Amount").Attributes("BenListId").Select(a => (int)a).ToList(),nm.Element("ListOfTaxableAllowance").Elements("Amount").Attributes("BenListId").Select(a => (int)a).ToList())

However i got an invalid initializer member declarator

XML Sample file.

  <EmployeeFinance>
    <EmpPersonal_Id>494</EmpPersonal_Id>
    <NonStatDedct>
      <DeductedAmt NonStatID="1037">0</DeductedAmt>
      <DeductedAmt NonStatID="106">5000</DeductedAmt>
    </NonStatDedct>
    <TotalDeduction>39909.83</TotalDeduction>
    <TotalTaxableEarnings>120054.27</TotalTaxableEarnings>
    <TotalNonTaxableEarnings>29500</TotalNonTaxableEarnings>
    <No_DaysWorked>21.667</No_DaysWorked>
    <Payperiod_EndDate>2014-02-28T00:00:00</Payperiod_EndDate>
    <Exchange_Rate>207.00</Exchange_Rate>
    <Currency>GYD</Currency>
    <Date_Appointment>2009-11-30T00:00:00</Date_Appointment>
    <Date_Employment>1994-12-01T00:00:00</Date_Employment>
    <Date_Termination>0001-01-01T00:00:00</Date_Termination>
    <Payperiod_StartDate>2014-02-01T00:00:00</Payperiod_StartDate>
    <BatchNumber>3192</BatchNumber>
    <PAYE_Free_Pay_Awarded>50000</PAYE_Free_Pay_Awarded>
    <Income_Tax_RateID>4</Income_Tax_RateID>
    <NIS_RateID>1</NIS_RateID>
    <Daily_Rate>5540.881</Daily_Rate>
    <NIS_weeks_worked>0</NIS_weeks_worked>
    <Incentive />
    <Retro>0</Retro>
    <ListOfTaxableAllowance>
      <Amount BenListId="4">0.00000</Amount>
      <Amount BenListId="0">0</Amount>
    </ListOfTaxableAllowance>
    <ListOfTNonaxableAllowance>
      <Amount BenListId="4">23500.00000</Amount>
      <Amount BenListId="0">0</Amount>
    </ListOfTNonaxableAllowance>
    <ListOfTaxableBenefits>
      <Amount BenListID="0">0</Amount>
    </ListOfTaxableBenefits>
    <ListOfNonTaxableBenefits>
      <Amount BenListID="0">0</Amount>
    </ListOfNonTaxableBenefits>
    <ListOfTaxableAddIncome>
      <Amount AddEarnID="14">0.00000</Amount>
    </ListOfTaxableAddIncome>
    <ListOfNonTaxableAddIncome>
      <Amount AddEarnID="14">6000.00000</Amount>
    </ListOfNonTaxableAddIncome>
    <ListOfNISdeductible>
      <Amount>0</Amount>
    </ListOfNISdeductible>
    <ListOfOtherTaxableAmount>
      <Amount>0</Amount>
    </ListOfOtherTaxableAmount>
    <ListOfPartofPension>
      <Amount>23500.00</Amount>
    </ListOfPartofPension>
  </EmployeeFinance>

Upvotes: 0

Views: 222

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

I think you need dictionary with BenListId attribute as key, and Amount as value:

TaxDiction = nm.Element("ListOfTaxableAllowance")
               .Elements("Amount")
               .ToDictionary(a => (int)a.Attribute("BenListId"),
                             a => (double)a)

Upvotes: 3

Related Questions