Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating IEnumerable<KeyValuePair<string,string>> values into string using Linq

Given IEnumerable<KeyValuePair<string,string>>, I'm trying to use linq to concatenate the values into one string.

My Attempt:

string path = attributes.Aggregate((current, next) => "@" + current.Key + "=" + current.Value + " and @" + next.Key + "=" + next.Value);

This produces the error:

Cannot convert expression type 'string' to return type 'KeyValuePair<string,string>'

Is there a more effiecient way to do this in linq?

The full method...

public IEnumerable<XmlNode> GetNodes(IEnumerable<KeyValuePair<string,string>> attributes) {
    StateInfoXmlDocument stateInfoXmlDocument = new StateInfoXmlDocument();
    string path = attributes.Aggregate((current, next) => "@" + current.Key + "=" + current.Value + " and @" + next.Key + "=" + next.Value);
    string schoolTypeXmlPath = string.Format(SCHOOL_TYPE_XML_PATH, path);

    return stateInfoXmlDocument.SelectNodes(schoolTypeXmlPath).Cast<XmlNode>().Distinct();
}
like image 812
bflemi3 Avatar asked Oct 19 '12 20:10

bflemi3


1 Answers

Is this what you're looking for?

var strings = attributes.Select(kvp => string.Format("@{0}={1}", kvp.Key, kvp.Value));
string path = string.Join(" and ", strings);
like image 153
Thomas Levesque Avatar answered Oct 06 '22 09:10

Thomas Levesque