C#如何从ilist中获取特定对象

时间:2016-10-04 16:27:48

标签: c# list

我根据年龄制作了一系列电影。如果您输入的年龄为18岁以上,您可以看到整个列表。如果你的年龄较小,你可以看到减少的名单,具体取决于你的年龄。

我用电影名称制作了一个列表,但在显示时不知道如何从列表中提取特定电影。

到目前为止,这是我的代码:

<!DOCTYPE html>
<html>
<body>
<div class="enter_stu_info">
<h2>Creating the following student records</h2>
First Name: testing Last name:testing Class_number: 1 Client_number: 7
<h3>Entered</h3>
<br>
<br>
"create_student_enter.php?client_number=7&class_no=1"
<br>
<input name="more_stu_button" onclick="window.location.replace(' create_student_enter.php?client_number=7&class_no=1');" value="Add more students" type="button">
</div>
<script type="text/javascript" language="javascript">
</body>

我似乎无法找到一个简单的答案,而我只是从整个编码世界开始。谢谢你的帮助!

2 个答案:

答案 0 :(得分:2)

public class Movie
{
   public int MinAge {get;set;}
   public string Name{get;set;}
}
var Movies = new List<Movie>{new Movie{Name = "blahblah", MinAge = 18}};
//create the list of movies with the age information
var filtered = (from m in Movies where m.MinAge >= 18 select m).ToList();

答案 1 :(得分:2)

可能您正在寻找一个能够保存您需要的电影信息的课程

class Movie
{
    public string Name { get; set; }
    public int AgeRestriction { get; set; }
}

然后根据该类填充列表并以您希望的方式返回结果

        Console.Write("Hi, if you wish to see a movie please enter your age: ");

        string AgeAsAString = Console.ReadLine();
        int Age = (int) Convert.ToInt32(AgeAsAString);

        List<Movie> ilist = new List<Movie>();

        ilist.Add(new Movie()
        {
            Name = "Buried",
            AgeRestriction = 18
        });

        ilist.Add(new Movie()
        {
            Name = "Despicable Me",
            AgeRestriction = 10
        });

        if (Age >= 18)
            return string.Join(",", ilist.Select(x => x.Name));
        else
            return string.Join(",", ilist.Where(x => x.AgeRestriction <= Age));

        Console.ReadKey();

我假设您需要将结果作为连接字符串而不是List。要过滤掉基于年龄的列表,请使用。

ilist.Where(x => x.AgeRestriction <= Age).ToList()