无法对我的EF4数据执行ORDERBY

时间:2010-05-03 16:08:20

标签: sql-server-2008 visual-studio-2010 linq-to-entities entity-framework-4

我有一个查询使用STEs点击EF4,我遇到了用户定义排序的问题。在调试这个时,我已经删除了动态排序并且对它进行了硬编码,我仍然遇到了问题。如果我在var results = xxx中交换/取消注释GetMyBusinesses()行,我的结果不会有任何不同的排序 - 他们总是按升序排序。

仅供参考,Name是我的Business表上SQL 2008中的varchar(200)字段。

private IQueryable<Business> GetMyBusinesses(MyDBContext CurrentContext)
{

    var myBusinesses = from a in CurrentContext.A
                       join f in CurrentContext.F
                           on a.FID equals f.id
                       join b in CurrentContext.Businesses
                           on f.BID equals b.id
                       where a.PersonID == 52
                       select b;

    var results = from r in myBusinesses
              orderby "Name" ascending
              select r;

    //var results = from r in results
    //          orderby "Name" descending
    //          select r;

    return results;
}

private PartialEntitiesList<Business> DoStuff()
{
    var myBusinesses = GetMyBusinesses();
    var myBusinessesCount = GetMyBusinesses().Count();

    Results = new PartialEntitiesList<Business>(myBusinesses.Skip((PageNumber - 1)*PageSize).Take(PageSize).ToList())
                  {UnpartialTotalCount = myBusinessesCount};

    return Results;
}

public class PartialEntitiesList<T> : List<T>
{
    public PartialEntitiesList()
    {
    }

    public PartialEntitiesList(int capacity) : base(capacity)
    {
    }

    public PartialEntitiesList(IEnumerable<T> collection) : base(collection)
    {
    }

    public int UnpartialTotalCount { get; set; }
}

1 个答案:

答案 0 :(得分:0)

显然将OrderBy子句作为字符串是无效的(我以为我之前已经测试过了?!)。如果我将Linq查询更改为以下内容,则可以正常运行:

var results = from r in myBusinesses
          orderby r.Name ascending
          select r;

这不能解决我的问题,但它确实回答了这个具体问题。我将发布与我的问题相关的另一个问题。