Can I use a TryParse inside Linq Comparable?

后端 未结 5 1481
野趣味
野趣味 2020-12-03 10:44

A sort of:

Documenti = Documenti
    .OrderBy(o => string.IsNullOrEmpty(o.Note))
    .ThenBy(o => Int32.TryParse(o.Note))
    .ToList();
5条回答
  •  天涯浪人
    2020-12-03 11:34

    Documenti = Documenti.OrderBy(o =>
            int.TryParse(o.Note, out int val)
                ? val
                : int.MaxValue /* or int.MinValue */
        ).ToList();
    

    Note: Toggling between int.MaxValue and int.MinValue will either put the empty values at the front or the end of the list.

    EDIT: 2020-02-07 Using an inline out variable which was introduced in C# 7

提交回复
热议问题