简体   繁体   中英

How to do order by child class property for a list of parent class?

Here parent class has child class in child class we have name how to make the list of parent as orderby using name property of child class. I used pq.OrderBy(z => z.Class1.Name != null).ToList(); but the list is not ordered as expected.

 class Program
        {
            static void Main(string[] args)
            {
                List<Parent> pq = new List<Parent>() {

                    new Parent () { Class1=new Child () { Name="d" } },
                    new Parent () { Class1=new Child () { Name="s" } },
                    new Parent () { Class1=new Child () { Name="y" } },
                    new Parent () { Class1=new Child () { Name="r" } },
                    new Parent () { Class1=new Child () { Name="b" } },
                    new Parent () { Class1=new Child () { Name="a" } }
                };

                var assa = pq.OrderBy(z => z.Class1.Name != null).ToList();
            }
        }

        public class Parent
        {
            public Child Class1 { get; set; }
        }

        public class Child
        {
            public string Name { get; set; }
        }

If you just want an ordered List you can use this:

var assa = pq.OrderBy(p => p.Class1.Name).ToList();

If it is possible that Class1 property is null use this:

var assa = pq.Where(p => p.Class1 != null).OrderBy(p => p.Class1.Name).ToList();

If you want to have those objects where Class1 is null at the end of the resulting List :

var assa = pq.Where(p => p.Class1 != null).OrderBy(p => p.Class1.Name).ToList();
assa.AddRange(pq.Where(p => p.Class1 == null));

use simply the property Name as the parameter for the call of the OrderBy function and you get your desired result:

var assa = pq.OrderBy(z => z.Class1.Name).ToList();

the problem in your code is that you give a boolean criterion which is used to determine the ordering. Since all elements in your list checked against this criterion will return true -> the order remains the same. You can test it by setting the Name of the last item to null .

new Parent () { Class1=new Child () { Name="d" } },
new Parent () { Class1=new Child () { Name="s" } },
new Parent () { Class1=new Child () { Name="y" } },
new Parent () { Class1=new Child () { Name="r" } },
new Parent () { Class1=new Child () { Name="b" } },
new Parent () { Class1=new Child () { Name=null } }

in this case your original query will result in an ordering of the last item as the first one

var assa = pq.OrderBy(z => z.Class1.Name != null).ToList();

The issue is your ordering function:

var assa = pq.OrderBy(z => z.Class1.Name != null).ToList();

If you notice, you are returning a boolean value from the function:

z => z.Class1.Name != null

What you want is to return the value of the Name property:

z => z.Class1.Name

Change it to this:

var assa = pq.OrderBy(z => z.Class1.Name).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM