0

I have a list of items in a SharePoint which has Title, Date and Priority. I would like to sort this collection on my client application while displaying it ,sorting need to first by Date Descending and then within a set of objects for a date, it should sort objects by Priority.

I am using below LINQ code, which is not providing the expected result. Means the below code not sorting object by priority with in a date.

 var results = spObjects.OrderByDescending(n => n.Date).ThenBy(t => t.Priority);

Unsorted collection is as below

enter image description here

Sorted Collection - Expectation

enter image description here

I am using SharePoint CAML query, since I am fetching these object from SP and I found it difficult to do it in CAML, but I assume this can be done using LINQ on C# side, Isnt it ? Could anyone please help ?

user1597990
  • 181
  • 3
  • 13

2 Answers2

0

If I understand you correctly you want a nested collection under a date heading, you might do something like this...

 var results = from so in spObjects
               orderby so.Date descending
               select new {
                    Date = so.Date,
                    Items = (from so2 in spObjects
                             where so2.Date == so.Date
                             orderby so2.Priority
                             select new {
                                  so2.Title,
                                  so2.Priority
                             })
                    };
spadelives
  • 1,588
  • 13
  • 23
  • thanks for replying, I tried this, not working. Once its is sorted by Date descending and then within that date, I need to sort by priority Ascending. – user1597990 Feb 16 '18 at 00:16
  • The purpose of the clause "orderby so2.Priority" is specifically to give you an ascending sort on priority in the nested query. – spadelives Feb 16 '18 at 00:22
  • Yes, checked but unfortunately its not working for me. For me Priority is an Enum which has {Urgent=1, Regualar =2 } . But I dont think it makes a difference, Is it ? – user1597990 Feb 16 '18 at 00:30
  • No, that doesn't matter since the enum is represented as an int in the database. Can you actually check the database and make sure the priorities are set correctly? Perhaps you have a problem with the code where you are displaying enums? – spadelives Feb 16 '18 at 01:20
0

I'll bet you that n.Date is a DateTime object, with hours, minutes, and seconds. Your Linq looks good, so you probably have different times that are causing items later in the day with lower priorities to be sorted before items earlier in the day.

Try something like:

 var results = spObjects.OrderByDescending(n => new DateTime(n.Date.Year, n.Date.Month, n.Date.Day)).ThenBy(t => t.Priority);
Jonathan
  • 4,916
  • 2
  • 20
  • 37