首页 > 解决方案 > 替换一个数组中包含在另一个数组中的值,而不遍历每条记录

问题描述

我正在通过抓取音乐节的网站艺术家页面来创建 Spotify 播放列表生成器。我能够返回所有艺术家姓名,但其中一些没有链接到 Spotify 搜索词,例如:

Festival Artist Name: "Slash and..."
Spotify Search Term:  "Slash"

我创建了一个名为EdgeCases

public int EdgeCasesID { get; set; }
public int FestivalID { get; set; }
public string ComparableString { get; set; }
public string SearchTerm { get; set; }
public bool Skip { get; set; }

它表示替换/跳过/搜索与 Spotify 搜索词不匹配的艺术家所需的所有数据。

我目前拥有的代码将所有边缘情况与从网页中提取的所有艺术家进行比较;我只想帮助如何减少代码+比较

foreach (string artist in artistNames)
{
    bool skipArtist = false;
    SearchItem searchItem = null;

    // Check if any of those artists exist in the UnfoundArtists database
    // More or less check if the name is different on Spotify or if they are even on Spotify
    foreach (EdgeCase unfoundArtist in EdgeCases)
    {
        // If the download artist page contains the a specific name 
        // e.g if 'slash & the conspirators' contains 'slash'
        if (artist.Trim().Contains(unfoundArtist.ComparableString)) 
        { 
            // If so, check if the artist is on Spotify
            if (unfoundArtist.ToSearch)
            {
                // search using SearchTerm
            }

            // This case is if the artist is on the page but not on Spotify
            skipArtist = true;
        }
        else
        {
            // Search the artist if there is no edge case - different Spotify 
            // name/on Spotify at all etc
            searchItem = _api.SearchItems(artist.Trim(), SearchType.Artist, limit: 1);
        }
    }
}

标签: c#

解决方案


推荐阅读