这是我第一次使用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));
现在我想找出所有被设置为父级的人。 但是我卡在这里:
var parents = people.Where()
var parents = people.Where(p => p.parent);
将返回所有parent
属性为true
的结果。在这种情况下,parent
是一个bool
类型,您可以在不使用==
或Equals
的情况下对其进行评估,但是对于非布尔类型的属性,您应该使用相等性比较。 - mihai