跳过可空对象的ThenBy



我有一个查询,应该像这样排序:

var list = new List<MonthClosureViewModel>();
var orderedList = list
    .OrderByDescending(x => x.Project)
    .ThenByDescending(x => x.ChargeLine)
    .ThenByDescending(x => x.DomesticSite) //<- x.DomesticSite might be null sometimes
    .ThenByDescending(x => x.ChargeSite)   //<- x.ChargeSite   might be null sometimes
    .ThenByDescending(x => x.RateGroup)
    .ThenByDescending(x => x.ApprovedHrs)
    .ThenByDescending(x => x.NotApprovedHrs);
public class MonthClosureViewModel
{
    public Project Project { get; set; }
    public ChargeLine ChargeLine { get; set; }
    public Site DomesticSite { get; set; }
    public Site ChargeSite { get; set; }
    public RateGroup RateGroup { get; set; }
    public decimal Rate { get; set; }
    public decimal ApprovedHrs { get; set; }
    public decimal NotApprovedHrs { get; set; }
}

但是如果任何对象是空的(完全由设计),这个查询失败。我怎么能把空值在结束或跳过排序,如果对象是空的?

补充道:@LasseV。卡尔森说我可能还有别的问题。我真的得到了ArgumentNullException,但原因不是在某些对象背后是null(我在调试器中看到它并错误地认为这是我的问题)。真正的原因是@RaphaëlAlthaus提到,我没有实现IComparable<>在我的任何类在MonthClosureViewModel

完成后,即使对象是null

,一切也开始正常工作。

您尝试按复杂属性排序,这(正如您提到的)是真正的问题。

要做到这一点,你必须:

  1. 在你的类中实现IComparable<T>

  2. 使用orderderby/orderbydescent的其他重载,也接受IComparer<TKey>作为参数(orderderby重载的msdn)

  3. 在order by子句中使用复杂属性的简单属性(null检查)。在这种情况下,null检查是必要的,以避免null引用异常):

例如:

.OrderByDescending(x => x.Project == null ? string.Empty : x.Project.Name)

您需要将defaultSitedefaultChargeSite定义为小于或大于其类中的所有其他对象

var list = new List<MonthClosureViewModel>();
var orderedList = list
     .Where(x => x != null);
    .OrderByDescending(x => x.Project)
    .ThenByDescending(x => x.ChargeLine)
    .ThenByDescending(x => x.DomesticSite==null?defaultSite:x.DomesticSite) //<- x.DomesticSite might be null sometimes
    .ThenByDescending(x => x.ChargeSite==null?defaultChargeSite:x.ChargeSite)   //<- x.ChargeSite   might be null sometimes
    .ThenByDescending(x => x.RateGroup)
    .ThenByDescending(x => x.ApprovedHrs)
    .ThenByDescending(x => x.NotApprovedHrs);

相关内容

  • 没有找到相关文章

最新更新