如何检查中的ObjectQuery℃的排序依据的presence; T>前pression树 [英] How to check for the presence of an OrderBy in a ObjectQuery<T> expression tree

查看:72
本文介绍了如何检查中的ObjectQuery℃的排序依据的presence; T>前pression树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用T4生成存储库LINQ到实体的实体。

I'm using T4 for generating repositories for LINQ to Entities entities.

存储库包含(除其他事项外)适用于分页列表的方法。支持的的文件和不支持的方法没有提到它,但你可以在一个无序的的IQueryable 'T呼跳过。这将引发以下异常:

The repository contains (amongst other things) a List method suitable for paging. The documentation for Supported and Unsupported Methods does not mention it, but you can't "call" Skip on a unordered IQueryable. It will raise the following exception:

System.NotSupportedException:该方法跳过仅支持
  在LINQ到实体分类输入。该方法排序依据必须之前调用
  该方法跳过。

System.NotSupportedException: The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'..

我通过允许来定义默认经由一个局部方法排序解决它。但我有问题,检查是否除权pression树确实包含一个排序依据

I solved it by allowing to define a default sorting via a partial method. But I'm having problems checking if the expression tree indeed contains an OrderBy.

我已经减少了问题的少code尽可能:

I've reduced the problem to as less code as possible:

public partial class Repository
{
    partial void ProvideDefaultSorting(ref IQueryable<Category> currentQuery);

    public IQueryable<Category> List(int startIndex, int count)
    {
        IQueryable<Category> query = List();
        ProvideDefaultSorting(ref query);
        if (!IsSorted(query))
        {
            query = query.OrderBy(c => c.CategoryID);
        }
        return query.Skip(startIndex).Take(count);
    }
    public IQueryable<Category> List(string sortExpression, int startIndex, int count)
    {
           return List(sortExpression).Skip(startIndex).Take(count);
    }
    public IQueryable<Category> List(string sortExpression)
    {
        return AddSortingToTheExpressionTree(List(), sortExpression);
    }
    public IQueryable<Category> List()
    {
           NorthwindEntities ent = new NorthwindEntities();
           return ent.Categories;
    }

    private Boolean IsSorted(IQueryable<Category> query)
    {
        return query is IOrderedQueryable<Category>; 
    }
}

public partial class Repository
{
    partial void ProvideDefaultSorting(ref IQueryable<Category> currentQuery)
    {
        currentQuery = currentQuery.Where(c => c.CategoryName.Contains(" ")); // no sorting..
    }
}

这是不是我的真实现!

但我的问题是,我怎么能贯彻 IsSorted 的方法?问题是,LINQ到实体查询的是类型的ObjectQuery ,总是它实现 IOrderedQueryable

But my question is, how could I implement the IsSorted method? The problem is that LINQ to Entities query's are always of the type ObjectQuery, which implements IOrderedQueryable.

所以,我应该如何确保一个排序依据方法是在EX pression树present?是解析树的唯一选择?

So how should I make sure an OrderBy method is present in the expression tree? Is the only option to parse the tree?

更新结果
我已经添加了其他两个重载明确指出,这不是关于如何排序支持添加到存储库,但如何检查 ProvideDefaultSorting 部分的方法的确增加了一个排序依据来恩pression树。

Update
I've added two other overloads to make clear that it's not about how to add sorting support to the repository, but how to check if the ProvideDefaultSorting partial method has indeed added an OrderBy to the expression tree.

的问题是,所述第一部分类是由一个模板和局部类的第二部分的执行产生是由在另一时间小组成员进行。您可以使用.NET实体框架生成的EntityContext的方式进行比较,它允许扩展点其他开发商。所以我想尽量做到稳定且不会崩溃时 ProvideDefaultSorting 不正确实施。

The problem is, the first partial class is generate by a template and the implementation of the second part of the partial class is made by a team member at another time. You can compare it with the way the .NET Entity Framework generates the EntityContext, it allows extension points for other developers. So I want to try to make it robust and not crash when the ProvideDefaultSorting is not implemented correctly.

所以,也许问题比较多,我怎么能确认 ProvideDefaultSorting 确实添加排序的前pression树。

So maybe the question is more, how can I confirm that the ProvideDefaultSorting did indeed add sorting to the expression tree.

更新2 结果
新的问题得到回答,并接受了,我觉得我应该改变其标题为更匹配的问题。或者我应该离开目前的标题,因为它会导致人同样的问题该解决方案?

Update 2
The new question was answered, and accepted, I think I should change the title to match the question more. Or should I leave the current title because it will lead people with the same problem to this solution?

推荐答案

您可以在ProvideDefaultSorting的返回类型解决这个问题。这code不建:

You can address this in the return type of ProvideDefaultSorting. This code does not build:

    public IOrderedQueryable<int> GetOrderedQueryable()
    {
        IQueryable<int> myInts = new List<int>() { 3, 4, 1, 2 }.AsQueryable<int>();
        return myInts.Where(i => i == 2);
    }

这code版本,但是是阴险和codeR得到他们应得的。

This code builds, but is insidious and the coder gets what they deserve.

    public IOrderedQueryable<int> GetOrderedQueryable()
    {
        IQueryable<int> myInts = new List<int>() { 3, 4, 1, 2 }.AsQueryable<int>();
        return myInts.Where(i => i == 2) as IOrderedQueryable<int>;
    }



同样的故事与REF(不建):

Same story with ref (this does not build):

    public void GetOrderedQueryable(ref IOrderedQueryable<int> query)
    {
        query = query.Where(i => i == 2);
    }

这篇关于如何检查中的ObjectQuery℃的排序依据的presence; T&GT;前pression树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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