How to write a program in C#, which searches through object list by any of the movie data (using loops)?

huangapple go评论73阅读模式
英文:

How to write a program in C#, which searches through object list by any of the movie data (using loops)?

问题

我正在使用C#创建一个简单的控制台应用程序,该应用程序具有使用循环搜索电影库(对象列表)中的任何电影数据(数据类型)的能力。

public static List<Movie> Movies= new List<Movie>()
{
(1, "The Shawshank Redemption", "Drama", 1994, "Tim Robbins", "9.3/10")
(2, "The Godfather", "Crime", 1972, "Marlon Brando", "9.2/10")
}


尝试一些方法但失去了主意:

public static void Search()
{
Console.WriteLine("输入搜索值: ");
string userInput = Console.ReadLine();
userInput.ToLower();

//foreach (var m in movies)
//{
//    if
//        (f.ID.ToString().Contains(userInput.ToLower()) || f.Title.Contains(userInput.ToLower()) || f.Year.ToString().Contains(userInput.ToLower()) || f.Genre.Contains(userInput.ToLower()) || f.mainActor.Contains(userInput.ToLower()) || f.IMDB_Rating.Contains(userInput.ToLower()))

foreach (Film f in movies)
{
    if(f.ToString().Contains(userInput))
    {
        Console.WriteLine(&quot;找到值!\n&quot;);
        Console.WriteLine($&quot;{f.MovieForPrintOut}&quot;);
    }
}

}


已经花了几个小时在YouTube、StackOverflow、OpenAI等网站上寻找最佳解决方案。

提前感谢,问候。
英文:

I am doing simple console app in C# which has ability to search through movie library (object list) by any movie data (data type) using loop.

public static List&lt;Movie&gt; Movies= new List&lt;Movie&gt;() 
{
(1, &quot;The Shawshank Redemption&quot;, &quot;Drama&quot;, 1994, &quot;Tim Robbins&quot;, &quot;9.3/10&quot;)
(2, &quot;The Godfather&quot;, &quot;Crime&quot;, 1972, &quot;Marlon Brando&quot;, &quot;9.2/10&quot;)
}

Try smth but lost idea:

public static void Search()
        {
            Console.WriteLine(&quot;Enter input value: &quot;);
            string userInput = Console.ReadLine();
            userInput.ToLower();

            //foreach (var m in movies)
            //{
            //    if
            //        (f.ID.ToString().Contains(userInput.ToLower()) || f.Title.Contains(userInput.ToLower()) || f.Year.ToString().Contains(userInput.ToLower()) || f.Genre.Contains(userInput.ToLower()) || f.mainActor.Contains(userInput.ToLower()) || f.IMDB_Rating.Contains(userInput.ToLower()))

            foreach (Film f in movies)
            {
                if(f.ToString().Contains(userInput))
                {
                    Console.WriteLine(&quot;Value find!\n&quot;);
                    Console.WriteLine($&quot;{f.MovieForPrintOut}&quot;);
                }
            }

Already spent hours looking for best solution on youtube, stackoverflow, openAI, etc.
Thanks in advance,
greeting.

答案1

得分: 2

如果我理解正确,我认为您所陈述的想法类似下面的代码。

请注意,在某些情况下,我们可能希望进行精确匹配,比如对于 YearId。否则,没有不区分大小写的 Contains 方法,但 IndexOf 接受一个比较器,因此我们可以使用该方法以不区分大小写的方式检查部分匹配(如果找到匹配的字符串,它会返回该字符串的索引,否则返回 -1)。

// 采用用于查找匹配的电影列表的字符串
// 返回电影列表
public static List<Movie> Search(string input)
{
    List<Movie> matches = new List<Movie>();

    foreach (var movie in Movies)
    {
        if (movie.Id.ToString().Equals(input) ||
            movie.Title.IndexOf(input, StringComparison.OrdinalIgnoreCase) >= 0 ||
            movie.Year.ToString().Equals(input) ||
            movie.Genre.IndexOf(input, StringComparison.OrdinalIgnoreCase) >= 0 ||
            movie.MainActor.IndexOf(input, StringComparison.OrdinalIgnoreCase) >= 0 ||
            movie.IMDBRating.IndexOf(input, StringComparison.OrdinalIgnoreCase) >= 0)
        {
            matches.Add(movie);
        }
    }

    return matches;
}

示例用法:

List<Movie> crimeMovies = Search("Crime");

这应该能让您入门,但结果可能有点不可预测。即使用户想要的是流派 "Crime",任何名称中包含 "Crime" 的电影都将被返回,所以您可能会得到一部名为 "热情的罪行" 的浪漫喜剧的结果。这是需要考虑的事情...

英文:

If I understand correctly, I think the idea you were stating was something like the code below.

Note that in some cases we probably want to do an exact match, like for Year or Id. Otherwise, there is no case-insensitive Contains method, but IndexOf takes a comparer, so we can use that method to check for partial matches in a case-insensitive way (it returns the index of the string to match if it was found, otherwise -1).

// Takes in a string that&#39;s used to find a list of movies that match
// The list of movies is returned
public static List&lt;Movie&gt; Search(string input)
{
    List&lt;Movie&gt; matches = new List&lt;Movie&gt;();

    foreach (var movie in Movies)
    {
        if (movie.Id.ToString().Equals(input) ||
            movie.Title.IndexOf(input, StringComparison.OrdinalIgnoreCase) &gt;= 0 ||
            movie.Year.ToString().Equals(input) ||
            movie.Genre.IndexOf(input, StringComparison.OrdinalIgnoreCase) &gt;= 0 ||
            movie.MainActor.IndexOf(input, StringComparison.OrdinalIgnoreCase) &gt;= 0 ||
            movie.IMDBRating.IndexOf(input, StringComparison.OrdinalIgnoreCase) &gt;= 0)
        {
            matches.Add(movie);
        }
    }

    return matches;
}

Sample usage:

List&lt;Movie&gt; crimeMovies = Search(&quot;Crime&quot;);

This should get you started, but the results will be a little unpredictable. Even though the Genre "Crime" was what the user wanted, any movie with "Crime" in the name will be returned, so you might get a Romantic Comedy named "Crimes of Passion" in the results. Something to think about...

huangapple
  • 本文由 发表于 2023年2月8日 19:42:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定