C#LINQ过滤列表

Mr.*_*ast 3 c# linq

这是我第一次使用LINQ而我还没有真正得到它.

我试图通过一个例子来理解它,但我需要一些帮助.

我创建了一个类"Person":

class Person
{
    private string name { get; set; }
    private int age { get; set; }
    private bool parent { get; set; }
    private bool child { get; set; }

    public Person(string name, int age, bool parent, bool child)
    {
        this.name = name;
        this.age = age;
        this.parent = parent;
        this.child = child;
    }
}
Run Code Online (Sandbox Code Playgroud)

我创建了一个"人物"列表:

people.Add(new Person("Joel", 12, false, true));
        people.Add(new Person("jana", 22, false, false));
        people.Add(new Person("Stefan", 45, true, false));
        people.Add(new Person("Kurt", 25, false, false));
        people.Add(new Person("Sebastian", 65, true, false));
        people.Add(new Person("George", 14, false, true));
        people.Add(new Person("Noel", 50, true, false));
Run Code Online (Sandbox Code Playgroud)

现在我想把所有被设定为父母的人都赶出去.但我被困在这里:

var parents = people.Where()
Run Code Online (Sandbox Code Playgroud)

fub*_*ubo 6

linq声明应该是

var parents = people.Where(x => x.parent);
Run Code Online (Sandbox Code Playgroud)

private bool parent { get; set; }改为public bool parent { get; set; }

  • 您需要将该物业公之于众. (2认同)