-1

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

3 Answers3

3

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));
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
1

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();
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • @BhuvaneshNarayanasamy how do you test it? `pq` will still have the same order. the returned list `assa` will have the ordered elements – Mong Zhu Jul 17 '17 at 14:16
1

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();
JuanR
  • 7,405
  • 1
  • 19
  • 30