---
title: "Using Key Value Pair with lists to dynamically create a paired value array"  
description: "Using Key Value Pair with lists to dynamically create a paired value array"  
author: "Anonymous User"  
published: 2014-08-14  
updated: 2014-08-14  
canonical: https://www.mindstick.com/forum/2033/using-key-value-pair-with-lists-to-dynamically-create-a-paired-value-array  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Using Key Value Pair with lists to dynamically create a paired value array

I'm currently using XDocument to read specific [attributes](https://www.mindstick.com/articles/13105/2-attributes-that-make-your-odoo-ecommerce-theme-productive-and-engaging) from a XML [Document](https://www.mindstick.com/articles/328642/what-to-consider-while-choosing-a-software-documentation-tool), This works without a hitch. Now, I'm trying to create an array with Keys and [Values](https://www.mindstick.com/forum/327/sum-textbox-values) to be later used for Validation. This may be an isolated [incident](https://answers.mindstick.com/qa/39513/who-did-not-vehemently-protested-the-withdrawal-of-the-non-cooperation-movement-after-the-chauri-chaura-incident) (due to being unable to [google](https://www.mindstick.com/articles/43833/google-lighthouse-and-how-is-it-changing-the-way-we-development-and-design-websites)/[research](https://www.mindstick.com/articles/311609/how-to-research-the-companies-before-applying-to-their-open-positions) the errors) , but the [error messages](https://www.mindstick.com/forum/160245/how-to-customize-error-messages-with-a-data-annotation-in-asp-dot-net-core) which I am experiencing has totally stumped me, the syntax does look correct. The code follows:

```
    public Array GetConfig(string FilePath)    {            var Document = XDocument.Load("CoreConfig.xml");            var Results = Document.Descendants().Elements("Database");            var ReturnList = new List<KeyValuePair<string,string>>();                string[] DBConfigElements = new string[4]{"Server","Username","Password","Database"};                int Count = 1;                var list = new List<KeyValuePair<string, string>>() { // Errors 1,2,3                foreach (string Elements in Results){                    new KeyValuePair<string,string>(DBConfigElements[Count],Elements);                    Count++;                } // Error 4            };      }
```

The error messages Presented are:

Error 1 Invalid initializer member declarator

Error 2 ; expected

Error 3 } expected

Error 4 { expected

I have labeled the code which line the [error message](https://www.mindstick.com/forum/23174/error-message-the-page-you-are-requesting-cannot-be-served-because-of-the-extension-configuration) is being triggered from. The syntax does look correct, so where is it that I am going wrong?

## Replies

### Reply by Pravesh Singh

Hi Ashish,\

You can't use foreach in an object initializer, but you can use Linq:

```
var list = new List<KeyValuePair<string, string>>(            Results.Select(r => new KeyValuePair<string,string>(DBConfigElements[Count++],r));
```


---

Original Source: https://www.mindstick.com/forum/2033/using-key-value-pair-with-lists-to-dynamically-create-a-paired-value-array

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
