gpt4 book ai didi

C# LINQ 过滤列表

转载 作者:行者123 更新时间:2023-11-30 13:30:18 24 4
gpt4 key购买 nike

这是我第一次使用 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;
}
}

我创建了一个“人”列表:

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));

现在我想找出每个被设置为 parent 的人。但我被困在这里:

var parents = people.Where()

最佳答案

linq语句应该是

var parents = people.Where(x => x.parent);

并更改private bool parent { get;放; }public bool parent { get;放; }

关于C# LINQ 过滤列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37188523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com