如何筛选有条件的孩子的父母名单

时间:2019-11-26 13:38:43

标签: c#

很抱歉写得不好... 我在mvc项目中有以下情形。 我有一个具有列表类型属性的对象,该属性还包含一个列表类型的子级。 如何获取List2子项的cod = 2和List3子项的cod = 9的List1对象。

   public class List1
    {
        public int cod {get;set;}
        public List<List2> list2 {get;set;}
    }

    public class List2
    {
        public int cod {get; set;}
        public List<List3> list3 {get;set;}

    }

    public class List3
    {
        public int cod {get;set;}
    }

    var list1 = new List1() { cod = 1 };
    list1.list2 = new List<List2>();
    list1.list2.Add(new List2
            {
                cod = 1,
                list3 = new List<List3>(){
                new List3{cod = 1},
                new List3{cod = 2}             
            },

            });

            list1.list2.Add(new List2
            {
                cod = 2,
                list3 = new List<List3>(){
                new List3{cod = 7},
                new List3{cod = 8},
                new List3{cod = 9}
            },

            });
            list1.list2.Add(new List2
            {
                cod = 2,
                list3 = new List<List3>(){
                new List3{cod = 7},            
                new List3{cod = 9},
                new List3{cod = 9}
            },

            });


            listFather.Add(list1);

2 个答案:

答案 0 :(得分:5)

我认为此版本更容易理解:

// How can I get the List1 object,
// where List2 children have cod = 2,
// and List3 children have cod = 9.

var matches = (from l1 in listFather
              from l2 in l1.list2
              from l3 in l2.list3
              where l2.cod == 2 &&
                    l3.cod == 9
              select l1).Distinct();

每当List1的List2 / List3组合匹配时,您将获得相同的List1实例,因此最后调用Distinct(),以便每个特定实例在结果中仅列出一次。

这是一个完整的例子:

public class List1
{
    public int cod { get; set; }
    public List<List2> list2 { get; set; }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("l1-cod: " + cod);
        foreach(List2 l2 in list2)
        {
            sb.AppendLine(l2.ToString());
        }
        return sb.ToString();
    }

}

public class List2
{
    public int cod { get; set; }
    public List<List3> list3 { get; set; }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("\tl2-cod: " + cod);
        foreach (List3 l3 in list3)
        {
            sb.AppendLine("\t\t" + l3.ToString());
        }
        return sb.ToString();
    }
}

public class List3
{
    public int cod { get; set; }

    public override string ToString()
    {
        return "l3-cod: " + cod;
    }
}

class Program
{

    static void Main(string[] args)
    {
        List<List1> listFather = new List<List1>();

        var listA = new List1() { cod = 1 };
        listA.list2 = new List<List2>();
        listA.list2.Add(new List2
        {
            cod = 1,
            list3 = new List<List3>(){
            new List3{cod = 1},
            new List3{cod = 2}
        },

        });

        listA.list2.Add(new List2
        {
            cod = 2,
            list3 = new List<List3>(){
            new List3{cod = 7},
            new List3{cod = 8},
            new List3{cod = 9}
        },

        });
        listA.list2.Add(new List2
        {
            cod = 2,
            list3 = new List<List3>(){
            new List3{cod = 7},
            new List3{cod = 9},
            new List3{cod = 9}
        },

        });
        listFather.Add(listA);

        var listB = new List1() { cod = 2 };
        listB.list2 = new List<List2>();
        listB.list2.Add(new List2
        {
            cod = 1,
            list3 = new List<List3>(){
            new List3{cod = 1},
            new List3{cod = 2}
        },

        });

        listB.list2.Add(new List2
        {
            cod = 2,
            list3 = new List<List3>(){
            new List3{cod = 7},
            new List3{cod = 8},
            new List3{cod = 10}
        },

        });
        listFather.Add(listB);

        var listC = new List1() { cod = 5 };
        listC.list2 = new List<List2>();
        listC.list2.Add(new List2
        {
            cod = 4,
            list3 = new List<List3>(){
            new List3{cod = 6},
            new List3{cod = 7}
        },

        });

        listC.list2.Add(new List2
        {
            cod = 2,
            list3 = new List<List3>(){
            new List3{cod = 7},
            new List3{cod = 8},
            new List3{cod = 10}
        },

        });
        listFather.Add(listC);

        var listD = new List1() { cod = 7 };
        listD.list2 = new List<List2>();
        listD.list2.Add(new List2
        {
            cod = 8,
            list3 = new List<List3>(){
            new List3{cod = 1},
            new List3{cod = 2}
        },

        });

        listD.list2.Add(new List2
        {
            cod = 2,
            list3 = new List<List3>(){
            new List3{cod = 7},
            new List3{cod = 9},
            new List3{cod = 1}
        },

        });
        listFather.Add(listD);

        Console.WriteLine("All objects:");            
        foreach(List1 l1 in listFather)
        {
            Console.WriteLine(l1);
        }
        Console.WriteLine("----------");

        // How can I get the List1 object,
        // where List2 children have cod = 2,
        // and List3 children have cod = 9.
        var matches = (from l1 in listFather
                      from l2 in l1.list2
                      from l3 in l2.list3
                      where l2.cod == 2 &&
                            l3.cod == 9
                      select l1).Distinct();

        Console.WriteLine("All matches:");
        foreach (List1 l1 in matches)
        {
            Console.WriteLine(l1);
        }
        Console.WriteLine();

        Console.Write("Press Enter to quit");
        Console.ReadLine();
    }

}

输出:

All objects:
l1-cod: 1
        l2-cod: 1
                l3-cod: 1
                l3-cod: 2

        l2-cod: 2
                l3-cod: 7
                l3-cod: 8
                l3-cod: 9

        l2-cod: 2
                l3-cod: 7
                l3-cod: 9
                l3-cod: 9


l1-cod: 2
        l2-cod: 1
                l3-cod: 1
                l3-cod: 2

        l2-cod: 2
                l3-cod: 7
                l3-cod: 8
                l3-cod: 10


l1-cod: 5
        l2-cod: 4
                l3-cod: 6
                l3-cod: 7

        l2-cod: 2
                l3-cod: 7
                l3-cod: 8
                l3-cod: 10


l1-cod: 7
        l2-cod: 8
                l3-cod: 1
                l3-cod: 2

        l2-cod: 2
                l3-cod: 7
                l3-cod: 9
                l3-cod: 1


----------
All matches:
l1-cod: 1
        l2-cod: 1
                l3-cod: 1
                l3-cod: 2

        l2-cod: 2
                l3-cod: 7
                l3-cod: 8
                l3-cod: 9

        l2-cod: 2
                l3-cod: 7
                l3-cod: 9
                l3-cod: 9


l1-cod: 7
        l2-cod: 8
                l3-cod: 1
                l3-cod: 2

        l2-cod: 2
                l3-cod: 7
                l3-cod: 9
                l3-cod: 1



Press Enter to quit

答案 1 :(得分:2)

让我尝试一下:

var list = listFather.Select(c => new List1
{
cod = c.cod,
list2 = c.list2.Select(d => new List2
{
cod = d.cod,
list3 = d.list3.Where(x => x.cod == 9).ToList()

}).Where(r => r.cod == 2).ToList()
}).ToList();