給定IEnumerable<KeyValuePair<string,string>>
,我嘗試使用linq將值連接成一個字符串。使用Linq將IEnumerable <KeyValuePair <string,string >>值連接到字符串中使用Linq
我嘗試:
string path = attributes.Aggregate((current, next) => "@" + current.Key + "=" + current.Value + " and @" + next.Key + "=" + next.Value);
這將產生錯誤:
Cannot convert expression type '
string
' to return type 'KeyValuePair<string,string>
'
有沒有更effiecient方式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();
}
所需轉換'string.ToArray()' – bflemi3
@ bflemi3,而不是在.NET 4.0或更高版本) –
很好解釋它,我在3.5 :) – bflemi3