簡體   English   中英

C#LINQ過濾列表

[英]C# LINQ filtering lists

這是我第一次使用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()

linq聲明應該是

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

並改變private bool parent { get; set; } private bool parent { get; set; } private bool parent { get; set; }public bool parent { get; set; } public bool parent { get; set; }

var peopleWithParents = people.Where(x => x.parent == true).ToList();

盡管可能需要一段時間才能解決問題,但語法非常簡單,並且使用起來非常舒適。 請記住,“where”充當過濾器,“select”充當投影。 請記住,您需要將屬性公開顯示。 如果你願意,你可以保持私人的私人。 您甚至可以引入一種方法來檢查父項是否存在,並在lambda中調用它。

假設您的人員課程更復雜。

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

    public bool HasParent()
    {
        return parent != null;
    }

    public Person(string name, int age, Person parent, bool child)
    {
        this.name = name;
        this.age = age;
        this.parent = parent;
        this.child = child;
    }
}

您可以執行以下操作來獲取表示所有父項的人員對象列表:

var parents = people.Where(x => x.parent != null).Select(x => x.parent).ToList();

要么

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

您也不需要使用'x',並且此字母僅在每個子句的范圍內; 甚至可以按年齡命令這些父母:

var parents = people.Where(y => y.HasParent()).Select(z => z.parent).OrderBy(x => x.age).ToList();

也許你想從上面的集合中獲得最老的父母:

var oldestParent = parents.OrderByDescending(x => x.age).FirstOrDefault();

if (oldestParent != null)
{
    Console.WriteLine($"{oldestParent.name} is the oldest, at {oldestParent.age}");
}

您可以在查詢中組合多個檢查:

var parentsOfPeopleWithChildren = people.Where(x => x.parent != null && x.child).Select(x => x.parent).ToList();

還記得在C#中你通常將首字母屬性大寫。 例如:

public bool Child { get; private set; }

LINQ非常簡單:

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

但是,由於其訪問修飾符 ,您的parent可能無法訪問

如果您不希望在聲明后從外部更改它(這可能是您通過構造函數的參數聲明屬性的原因)。 我建議你使用public { get; private set; } public { get; private set; }

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

    public Person(string name, int age, bool parent, bool child)
    {
        this.name = name;
        this.age = age;
        this.parent = parent;
        this.child = child;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM