locked
LINQ OrderBy RRS feed

  • Question

  • I have a webpart that queries from a SP List.

    I use LINQ to query. My list has sorting column (Number) that is used for sorting.

    If some entries in the list have null Sorting values, then those values should be at the bottom of the result, and all the values with Sorting values should be on top and should be sorted ascending.

    However, the result shows the values WITHOUT Sorting values first, and the values WITH Sorting comes next.

    How should I fix this?

    Here's my query

    List<SPListItem> results = list.Items.OfType<SPListItem>().Where(i => i["Parent"] != null &&
                                        (new SPFieldLookupValueCollection(i["Parent"].ToString())).OfType<SPFieldLookupValue>()
                                        .Where(v => v.LookupValue.Equals(parent)).Count() > 0)
                                        .OrderBy(o => Convert.ToInt32(o["Sorting"])).ThenBy(t => t["Child"])
                                        .Distinct().ToList();

    Here's the list and result: ---- letter G should be on top of letter H. How to do this?


    ----------------------- Sharepoint Newbie

    Wednesday, October 8, 2014 3:57 AM

All replies

  • Hi Greetings.

    Pls check if it can help

    http://stackoverflow.com/questions/6461479/linq-order-by-null-column-where-order-is-ascending-and-nulls-should-be-last

    http://stackoverflow.com/questions/13604630/c-sharp-linq-orderby-filtering-null-or-empty-values-to-be-last


    Please remember to click 'Mark as Answer' on the answer if it helps you

    Wednesday, October 8, 2014 5:29 AM
  • Thanks for the links but .HasValue doesn't show up in my intellisense

    ----------------------- Sharepoint Newbie

    Thursday, October 9, 2014 1:30 AM