locked
Multiple Sorting with GroupJoin is not working as expected RRS feed

  • Question

  • Hi All,

    It may sound bit weird however i am facing issue with a basic operation in gropujoin.

    I have tow Lists, List1 and List2. List 1 is very large list with morethan 250 properties.

    I want to get my List 1 sorted with 2 fields of List 2 and 1 field from List1. Both the list has a common field.

    following is my join code

                var        result= List1.GroupJoin(List2, l=> l.commonField, r=> r.commonField,
                       (x, y) => new
                       {
                           List1Obj= x,
                           List2SelectedFields= y.Select(wc => new { wc.OrderField1, wc.commonField}).FirstOrDefault()
                       }).OrderBy(x => x.List2SelectedFields.OrderField1)

                          .ThenBy(x => x.List2SelectedFields.commonField)

                         .ThenBy(x => x.List1Obj.sortField)
                       .Select(x => x.List1Obj).ToList();

    I have checked the same code with different sample where it works fine.

    Both the list were Observable Collections however i am converting them to List before groupjoin.

    Can someone help me on am i missing anything?

    Thank You!



    Best Regards, Sunil Sharma

    Friday, June 26, 2020 3:19 PM

Answers

  • Hi All,

    Thank you for the response and help. Appreciate it.

    I could debug and resolve the issue. I added the Comparer implementation to debug it and found that the date field had no values assigned and it was getting assigned during the completion of first load hence it was getting sorted at 2nd time.

    Thank you for your co-operation. 


    Best Regards, Sunil Sharma

    • Marked as answer by Sunilsharma Tuesday, June 30, 2020 5:51 AM
    Tuesday, June 30, 2020 5:51 AM

All replies

  • Can you remove the very last .Select statement and do ToList() after last ThenBy ?

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    Friday, June 26, 2020 3:28 PM
  • Hi Naomi,

    Thanks for the reply. It did not resolve the issue.

    I have to show the result in gridview in WPF application. When i pop the window with this result for first time, It sorts the data as per the first 2 sort fields, however it doesn't sort by third field which is date time.

    When i open the window 2nd time it sorts correctly.

    I have also checked that Even IF i sort the List directly with third field it dosnt sort it first time, However it does the second time. Its so weird. Not sure whiat is causing this behaviour.

    Thank You!  


    Best Regards, Sunil Sharma

    Friday, June 26, 2020 3:43 PM
  • How these lists were originally produced and how many elements each list has? Are they truly simple collections or there is some query executed behind the scenes?

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    Friday, June 26, 2020 4:50 PM
  • hi

     private void testgroupjoin()
            {
                List<sales> List1 = new List<sales>();
                sales s1 = new sales();
                s1.commonField = "A";
                s1.sale = 2000.00;
                List1.Add(s1);
                sales s2 = new sales();
                s2.commonField = "B";
                s2.sale = 3000.00;
                List1.Add(s2);
                sales s3 = new sales();
                s3.commonField = "C";
                s3.sale = 4000.00;
                List1.Add(s3);
                List<purchases> List2 = new List<purchases>();
                purchases p1 = new purchases();
                p1.commonField = "A";
                p1.purchase = 1000.00;
                List2.Add(p1);
                purchases p11 = new purchases();
                p11.commonField = "B";
                p11.purchase = 2000.00;
                List2.Add(p11);
                var result = List1.GroupJoin(List2, l => l.commonField, r => r.commonField,
                                   (x, y) => new
                                   {
                                       List1Obj = x,
                                       List2SelectedFields = y.Select(wc => new { wc.purchase, wc.commonField }).FirstOrDefault()
                                   }).OrderBy(x => x.List1Obj.sale)
                                    .ThenBy(x => x.List1Obj.commonField)
                                   .Select(x => x.List1Obj).ToList();
            }
        }
        class sales
        {
          public  string commonField { get; set; }
            public double sale { get; set; }
        }
        class purchases
        {
            public string commonField { get; set; }
            public double purchase { get; set; }
        }
    
    
    
    

    you may select

     .Select(x => x.List2selectedFields).ToList();

    Friday, June 26, 2020 5:33 PM
  • Hello,

    Create a unit test project, create base class which if nothing else has mocked data that represents data used in your lambda statement that when tested a) validates or invaliates what you expect and if still stuck you can either push the test project to a new GitHub repository or upload to Microsoft Onedrive in a .ZIP file for others to work with.

    If you don't know how to unit test, this is a great time to learn. Pretty much all code I write for work goes one step deeper "Test driven development" (TDD) which the first few times working this way is super frustrating for most developers and is time consuming but has a good deal of ROI now and later down the road.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, June 26, 2020 5:56 PM
  • Hi All,

    Thank you for the response and help. Appreciate it.

    I could debug and resolve the issue. I added the Comparer implementation to debug it and found that the date field had no values assigned and it was getting assigned during the completion of first load hence it was getting sorted at 2nd time.

    Thank you for your co-operation. 


    Best Regards, Sunil Sharma

    • Marked as answer by Sunilsharma Tuesday, June 30, 2020 5:51 AM
    Tuesday, June 30, 2020 5:51 AM