如何在实体框架中最后按具有空值的列排序 [英] How to order by column with null values last in entity framework

查看:61
本文介绍了如何在实体框架中最后按具有空值的列排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何最后返回具有空值的字符串的有序列表吗?我有这样的东西:

Does anyone know how to return an ordered list of strings with null values last? I have something like this:

using(var context = new DomainEntities())
{
    var result = context.Users.OrderBy(u => u.LastName).ThenBy(u => u.FirstName);
}

我的问题是,此查询在非空值之前返回空值。

My problem though is that this query returns null values before non-null values.

有什么想法吗?

推荐答案

我会做:

using(var context = new DomainEntities())
{
    var result = context.Users.OrderBy(u => u.LastName == null)
                              .ThenBy(u => u.LastName)
                              .ThenBy(u => u.FirstName == null)
                              .ThenBy(u => u.FirstName);
}

...这应该产生合理的SQL。

...which should produce reasonable SQL.

编辑:解释(摘自Craig的评论):

explanation (taken from Craig's comment):

因为 false 之前排序 true

这篇关于如何在实体框架中最后按具有空值的列排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆