相关疑难解决方法(0)

LINQ中的多个"order by"

我有两个表,movies并且categories,我首先按类别ID获取有序列表,然后按名称获取.

影片表有三列,ID,Name和CategoryID.类别表2包含列,ID和名称.

我尝试了类似下面的内容,但它没有用.

var movies = _db.Movies.OrderBy( m => { m.CategoryID, m.Name })
Run Code Online (Sandbox Code Playgroud)

linq sql-order-by

1537
推荐指数
7
解决办法
58万
查看次数

如何在实体框架中查询空值?

我想执行这样的查询

   var result = from entry in table
                     where entry.something == null
                     select entry;
Run Code Online (Sandbox Code Playgroud)

并得到一个IS NULL生成.

编辑:在前两个答案后,我觉得有必要澄清我正在使用实体框架而不是Linq to SQL.object.Equals()方法似乎不适用于EF.

编辑2:上述查询按预期工作.它正确生成IS NULL.然而,我的生产代码是

value = null;
var result = from entry in table
                         where entry.something == value
                         select entry;
Run Code Online (Sandbox Code Playgroud)

并且生成的SQL是something = @p; @p = NULL.似乎EF正确地转换了常量表达式,但是如果涉及变量,它就像正常比较一样处理它.实际上是有道理的.我会结束这个问题

.net ado.net entity-framework

107
推荐指数
6
解决办法
10万
查看次数

LINQ按降序排序,底部为空值

我有这个表达式:

troubletickets = db.ServiceTickets.Include(t => t.Company).Include(t => t.UserProfile);
troubletickets.OrderByDescending(t => t.UserProfile != null ? t.UserProfile.FirstName : "ZZZ");
Run Code Online (Sandbox Code Playgroud)

我必须检查UserProfile是否为null,因为如果我不这样做,我将收到错误.问题是,有时UserProfiles.FirstName可以为null.当它为null时,当我按升序和降序排序时,这些值将放在列表的顶部.例如

// Null, Null, Andy, Bill, Chris
// Null, Null, Chris, Bill, Andy
Run Code Online (Sandbox Code Playgroud)

我怎样才能改变这个表达式,这样当我按降序排序时,它会返回类似这样的内容:

// Chris, Bill, Andy, Null, Null
Run Code Online (Sandbox Code Playgroud)

c# linq asp.net-mvc entity-framework

15
推荐指数
2
解决办法
3万
查看次数

在动态Linq Order By上保留NULL行

我正在使用下面的代码片段动态地订购我的Linq查询并且效果很好.我在反射或复杂的linq查询方面不是很出色,但我需要一种方法,当使用升序时,NULL值是最后一个,反之亦然.

因此,如果我的属性名称是一个整数且列值为1,3,5,那么所有NULL行都将在结尾处,而不是默认情况下的开头.我可以添加什么来表达这种情况?

此代码适用于实体框架,仍然需要进行NULL比较.

list.OrderBy("NAME DESC").ToList()
Run Code Online (Sandbox Code Playgroud)

   public static class OrderByHelper
    {
        public static IOrderedQueryable<T> ThenBy<T>(this IEnumerable<T> enumerable, string orderBy)
        {
            return enumerable.AsQueryable().ThenBy(orderBy);
        }

        public static IOrderedQueryable<T> ThenBy<T>(this IQueryable<T> collection, string orderBy)
        {
            if (string.IsNullOrWhiteSpace(orderBy))
                orderBy = "ID DESC";

            IOrderedQueryable<T> orderedQueryable = null;

            foreach (OrderByInfo orderByInfo in ParseOrderBy(orderBy, false))
                orderedQueryable = ApplyOrderBy<T>(collection, orderByInfo);

            return orderedQueryable;
        }

        public static IOrderedQueryable<T> OrderBy<T>(this IEnumerable<T> enumerable, string orderBy)
        {
            return enumerable.AsQueryable().OrderBy(orderBy);
        }

        public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> collection, string orderBy)
        {
            if (string.IsNullOrWhiteSpace(orderBy))
                orderBy …
Run Code Online (Sandbox Code Playgroud)

.net c# linq reflection

12
推荐指数
1
解决办法
1028
查看次数