6

を与えられIEnumerable<KeyValuePair<string,string>>て、私はlinqを使用して値を1つの文字列に連結しようとしています。

私の試み:

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

これにより、エラーが発生します。

string式タイプ' 'を戻りタイプ''にKeyValuePair<string,string>変換できません

linqでこれを行うためのより効率的な方法はありますか?

完全な方法...

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();
}
4

4 に答える 4

17

これはあなたが探しているものですか?

var strings = attributes.Select(kvp => string.Format("@{0}={1}", kvp.Key, kvp.Value));
string path = string.Join(" and ", strings);
于 2012-10-19T20:33:07.133 に答える
4
string s = String.Join("@",attributes.Select(kv=>kv.Key+"="+kv.Value));
于 2012-10-19T20:33:41.350 に答える
0

集約を使用して文字列を作成する場合、シードされていないバージョンを使用する場合は、集約のシードされたオーバーロードを使用する必要があります。呼び出しのすべてのタイプは同じである必要があります。

于 2012-10-19T20:33:44.440 に答える
0
string templ = "{0}={1}";
string _authStr = String.Join("&", formParams.Select(kv => String.Format(templ, kv.Key, kv.Value));
于 2015-06-23T11:55:43.820 に答える