我正在尝试提供一种搜索特定曲目的方法,如果有任何匹配,则返回整个匹配的CD.
这是我到目前为止的尝试:
public CompilationCD FindTrackInComCD(string track) { CompilationCD temp = new CompilationCD(); List<CD> tempComCols = _cdCollection.FindAll(delegate(CD cd) { return cd.GetType() == temp.GetType(); }); foreach (KeyValuePair<string, string> comCD in tempComCols) { if (comCD.Key.Contains(track)) { return comCD; } } throw new ArgumentException("No matches found"); }
我有一个CD类型的CD集合(List< CD>)因此我创建了一个新的List<>通过将它与临时列表进行比较来获得适当的类型.
编译时我收到以下错误:
Cannot convert type 'CDCollection.CD' to System.Collections.Generic.KeyValuePair<string,string>' Cannot implicitly convert type 'System.Collections.Generic.KeyValuePair<string,string>'
(CDCollection是我的项目命名空间,CD / CompilationCD是类)
对不起,这似乎与我之前提出过的问题类似.我尝试使用之前给出的方法,但我有点难过;我没有使用List<>或KeyValuePair经常.
这是CD类:
using System;
使用System.Collections;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
命名空间CDCollection
{
公共课CD
{
#region Fields
private readonly string _artist;
private readonly string _album;
private List _track = new List();
#endregion
#region Constructors public CD() { _artist = ""; _album = ""; _track = null; } public CD(string albumName) { _album = albumName; } public CD(string artistName, string albumName) { _artist = artistName; _album = albumName; } #endregion #region Properties /// <summary> /// Get/Set Artist Name /// </summary> public virtual string Artist { get { return _artist; } set { value = _artist; } } /// <summary> /// Get/Set Album /// </summary> public string Album { get { return _album; } set { value = _album; } } /// <summary> /// Get/Set Track Name /// </summary> public virtual List<string> Track { get { return _track; } set { value = _track; } } #endregion #region ToString() /// <summary> /// Custom ToString() Method /// </summary> /// <returns></returns> public override string ToString() { //Create new StringBuilder object StringBuilder sb = new StringBuilder(); sb.Append("Artist Name"); //Display error if Artist is not available if (_artist == null || _artist == "") { sb.Append("\nNo Artist Entered"); } else { sb.Append("\n" + this._artist); } sb.Append("\n"); sb.Append("\nAlbum Name"); //Display error if Album is not available if (_album == null || _album == "") { sb.Append("\nNo Album Entered"); } else { sb.Append("\n" + this._album); } sb.Append("\n"); sb.Append("\nTrack Name"); sb.Append("\n"); //Iterate through all tracks stored in list foreach (string trackName in _track) { //Print each artist sb.Append("\n" + trackName); } sb.Append("\nEnd of CD Record........."); return sb.ToString(); } #endregion }
}
这是CompilationCD类:
using System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
命名空间CDCollection
{
公共类编译CD:CD
{
#region Fields
private readonly string _artist; private readonly string _album; private List<KeyValuePair<string,string>> _tracks = new List<KeyValuePair<string,string>>(); //List<KeyValuePair> Reference. //http://msdn.microsoft.com/en-us/library/6sh2ey19(VS.85).aspx #endregion #region Constructors public CompilationCD() { _album = ""; _artist = "Various Artists"; } public CompilationCD(string albumName):base(albumName) { _album = albumName; _artist = "Various Artists"; } #endregion public void AddTracks(string track, string artist) { _tracks.Add(new KeyValuePair<string, string>(track, artist)); } #region Properties public override string Artist { get { return this._artist; } } public new List<KeyValuePair<string,string>> Track { get { return _tracks; } set { _tracks = value; } } #endregion #region ToString() //TEST public override string ToString() { //Create new StringBuilder object StringBuilder sb = new StringBuilder(); sb.Append("Artist Name"); //Display error if Artist is not available if (_artist == null || _artist == "") { sb.Append("\nNo Artist Entered"); } else { sb.Append("\n" + this._artist); } sb.Append("\n"); sb.Append("\nAlbum Name"); //Display error if Album is not available if (base.Album == null || base.Album == "") { sb.Append("\nNo Album Entered"); } else { sb.Append("\n" + base.Album); } sb.Append("\n"); sb.Append("\nTrack Name"); sb.Append("\n"); ////Iterate through all tracks stored in list //foreach (string trackName in base.Track) //{ // //Print each artist // sb.Append("\n" + trackName); //} for(int i = 0; i <= _tracks.Count; i++) { string track = _tracks[i].Key; string artist = _tracks[i].Value; sb.Append("\nTrack"); sb.Append(track); sb.Append("\nArtist"); sb.Append(artist); } sb.Append("\nEnd of Compilation CD Record........."); return sb.ToString(); } #endregion }
}
我有严格的规则,这意味着我必须从CD继承以创建我的CompilationCD以及使用List>对于我的曲目集,它需要同时拥有曲目和艺术家.疯了我知道= /
此外,我必须将所有类型的CD存储在CD(List)类型的列表中.
问题出在你的foreach循环中. tempComCols是List< CD>,但comCD是KeyValuePair< string,string>.因此,您的循环导致无效的类型转换.不幸的是,由于我们不知道CD类(接口?)是什么样的,我不能建议修复它的属性.
编辑:以下可能是您的方法的更好版本(但是,我没有正确调试它):
public CompilationCD FindTrackInComCD(string track) { CompilationCD temp = new CompilationCD(); temp = _cdCollection.Where(cd => cd is CompilationCD) .Cast<CompilationCD>() .Where(com_cd => com_cd.Tracks.ContainsKey(track)) .FirstOrDefault(); if (temp != null) return temp; else throw new ArgumentException("No matches found"); }
你不能将CompilationCD转换为KeyValuePair< string,string>,所以我们只是直接使用CompilationCD类.我们可以将搜索轨道列表的责任转移到IEnumerable< T> System.Linq提供的扩展方法,使这个方法非常简单.