English 中文(简体)
驱逐令 摘自IQueryable<T>
原标题:Remove OrderBy from an IQueryable<T>

我有一张寄生机,使用户要求回归,但每次只收到这么多,而不是全部收集。 经设计的APIC工程,但我确实必须计算现有记录总数(适当页数计算)。 在APIC中,我使用Linq2Sql,我在最后提出我的要求之前,与I Queryable进行了大量工作。 当我拿到这笔钱时,我说的是这样的东西:RecordCount=可问。

由此而来的一点并不令人感兴趣,但它也增加了一项不必要的命令,使问题变得非常昂贵。

exec sp_executesql N SELECT COUNT(*) AS [value]
FROM (
    SELECT TOP (1) NULL AS [EMPTY]
    FROM [dbo].[JournalEventsView] AS [t0]
    WHERE [t0].[DataOwnerID] = @p0
    ORDER BY [t0].[DataTimeStamp] DESC
    ) AS [t1] ,N @p0 int ,@p0=1

由于我正在使用可贵的电文,因此,我可以先操纵可贵的电文,然后再将其操作到服务器上。

我的问题是,如果我已经具备一项命令,那么在我打电话之前能否取消这一命令?

∗∗∗∗∗∗∗

如果没有,就没有大的精力。 我看到许多问题是如何命令By的,但不涉及从Linq的表述中删除命令By。

Thanks!

问题回答

因此,以下法典是对一个内层阵列的猛增。 可能有一些障碍,难以与实体框架合作(或执行某些其他武断的教益)。 从根本上说,我们将做的是参观表达的树木,寻找任何传教方法,简单地将其从树中删除。 希望能把你引向正确方向。

class Program
{
    static void Main(string[] args)
    {
        var seq = new[] { 1, 3, 5, 7, 9, 2, 4, 6, 8 };

        var query = seq.OrderBy(x => x);

        Console.WriteLine("Print out in reverse order.");
        foreach (var item in query)
        {
            Console.WriteLine(item);
        }

        Console.WriteLine("Prints out in original order");
        var queryExpression = seq.AsQueryable().OrderBy(x => x).ThenByDescending(x => x).Expression;

        var queryDelegate = Expression.Lambda<Func<IEnumerable<int>>>(new OrderByRemover().Visit(queryExpression)).Compile();

        foreach (var item in queryDelegate())
        {
            Console.WriteLine(item);
        }


        Console.ReadLine();
    }
}

public class OrderByRemover : ExpressionVisitor
{
    protected override Expression VisitMethodCall(MethodCallExpression node)
    {
        if (node.Method.DeclaringType != typeof(Enumerable) && node.Method.DeclaringType != typeof(Queryable))
            return base.VisitMethodCall(node);

        if (node.Method.Name != "OrderBy" && node.Method.Name != "OrderByDescending" && node.Method.Name != "ThenBy" && node.Method.Name != "ThenByDescending")
            return base.VisitMethodCall(node);

        //eliminate the method call from the expression tree by returning the object of the call.
        return base.Visit(node.Arguments[0]);
    }
}

There isn t just an unneeded ORDER BY, there s also a spurious TOP(1).

SELECT TOP (1) NULL AS [EMPTY] ...

这一次选举只能返回0或1行。 事实上,如果没有联塔办事处,就没有法律根据代言而立。

按条款分列的书面记录在意见、在线功能、衍生表格、子类和共同表格表述中无效,除非还具体指明: SlectT COUNT(*) 目 录

sqlfiddle

我认为,你或许在《准则》中做了一些错误的事情。 您在打字前是否在打字栏上打字了打字,然后打上了<代码>。

这是错误的:

IQueryable<Foo> foo = (...).OrderBy(x => x.Foo).Take(1);
int count = foo.Count();

我们应该这样做:

IQueryable<Foo> foo = (...);
Iqueryable<Foo> topOne = foo.OrderBy(x => x.Foo).Take(1);
int count = foo.Count();

恐怕没有轻易的办法去除<代码>。 OrderBystart from queryable.

但是,你可以做的是重新编号<代码>。 I Queryable based on the new expression received from re写作 queryable.Expression ( see here > omitting the OrderBy

如果你能够消除根源,那就是一个工作:

totalRecordCount = queryable.OrderBy(x => 0).Count();

页: 1 服务器梯度优化器将消除这种无用的订购。 该公司获得一定时间的费用。

我认为,你执行过错误的编码。 确实,你需要两次查询数据库,一次询问数据来源,一次询问总浏览量。 这正是这些设置应如何看待的。

public IList<MyObj> GetPagedData(string filter, string sort, int skip, int take)
{
   using(var db = new DataContext())
   {
      var q = GetDataInternal(db);
      if(!String.IsNullOrEmpty(filter))
         q = q.Where(filter); //Using Dynamic linq

      if(!String.IsNullOrEmpty(sort))
         q = q.OrderBy(sort); //And here

      return q.Skip(skip).Take(take).ToList();
   }
}

public int GetTotalCount(string filter)
{
    using(var db = new DataContext())
    {
       var q = GetDataInternal(db);
       if(!String.IsNullOrEmpty(filter))
         q = q.Where(filter); //Using Dynamic linq

       return q.Count(); //Without ordering and paging.
    }
}

private static IQuerable<MyObj> GetDataInternal(DataContext db)
{
   return 
        from x in db.JournalEventsView 
        where ...
        select new ...;
}

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx”rel=“nofollow”

我知道,这并不是你所期待的,而是列入数据时间统计的[数据所有权指数]指数,可能会降低你的询问费用。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...